Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

How do i call a dll which is created in .net framework 2.0 in C++,
although the dll is not COM registered. With registering the dll i can call the dll with the following code.

#include "stdio.h"
#include "conio.h"

#import "C:\Documents and Settings\Desktop\d\test.dll.tlb"

using namespace test;

#include <string>;
using namespace std;

void main(int argc, char* argv[])
{
  HRESULT hr = CoInitialize(NULL);
  IPayProcess* pTest = NULL;
  hr = CoCreateInstance(__uuidof(Pay), NULL, CLSCTX_INPROC_SERVER, __uuidof(IPayProcess), (void**)&pTest);
  pTest->;PayProcess("abc");
}
</string>


If I don't register the dll it gives the object reference not set to an instance error.

Thanks
Posted
Updated 28-Oct-10 1:12am
v2

Have a look Registration-Free COM Interop:
http://msdn.microsoft.com/en-us/library/fh1h056h.aspx[^]

And also the DllImportAttribute Class:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^]

Good luck!
 
Share this answer
 
You can use CoLoadLibrary() to load the dll and obtain a reference to it with the following code; just add the path to your dll to the first parameter. Also change references to CLSID_CGeneric and IID_IGeneric to your own GUID references.

Note that you should always check the return code from CoCreateInstance() before trying to use the pointer.

// check the return code
if (hr == REGDB_E_CLASSNOTREG)
{
    // try CoLoadLibrary...
    HINSTANCE hInstance = CoLoadLibrary([path to your DLL], FALSE);
    if (hInstance != NULL)
    {
        typedef HRESULT (__stdcall *pDllGetClassObject)(REFCLSID rclsid, REFIID riid, PVOID* ppv);
			
        pDllGetClassObject GetClassObject = (pDllGetClassObject)GetProcAddress(hInstance, "DllGetClassObject");
        if (GetClassObject != NULL)
        {
            IClassFactory*	pIFactory;

            hr = GetClassObject(CLSID_CGeneric, IID_IClassFactory, (PVOID*)&pIFactory);

            if (SUCCEEDED(hr))
            {
                hr = pIFactory->CreateInstance(NULL, IID_IGeneric, (PVOID*)&pGeneric);
                pIFactory->Release();
            }
        }
    }
}
if (SUCCEEDED(hr))
{
    // use the COM object as required
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900