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

Here is my code
C++
#import "sample.dll" //This is com dll

using namespace SampleLib; // This is in the dll

main()
{
SampleLib::ISampleLibPtr m_objDll; //The variable ISampleLibPtr is typedef 

m_objDll.CreateInstance("PROG_ID");

m_objDll->create();
}

The above code work fines.
Here i am importing the dll in the code itself.
But i case is , There are many com dll with different version implementing the same interface. So based on the given version i have to select the related com dll and have to import and then i have to call the create interface.

Can any one help on this?
Posted
Updated 5-Oct-12 5:02am
v2

Hello,

You have to know COM interface of the object to work with, or at least the one or few methods what are necessary for you.
The COM dll you can load dynamically and the dll isn't necessary to be registered.

Here is code example, hope that is what you were mean in your question:

CoInitialize(NULL); // Should be called somethere before  in thread
HMODULE hDll = LoadLibrary(_T("Your_Dll.dll")); // You can also pecify the path - full or relative
if (hDll)
{
	typedef HRESULT (WINAPI * pfnDLLGetClassObject)(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
	// DllGetClassObject function should be imported for COM library
	pfnDLLGetClassObject fnDLLGetClassObject = (pfnDLLGetClassObject)GetProcAddress(hDll,"DllGetClassObject");
	const CLSID _MyObjectCLSID = CLSID_MyObjectInDll; // Your COM Object CLSID in that DLL
	const IID _MyObjectInterfaceIID = IID_IMyObject; // Your COM object interface
	// You can use __uuidof operator in 2 strings above
	IClassFactory * pFactory;
	// Here we obtaining the Class Factory for your object
	hr = fnDLLGetClassObject(_MyObjectCLSID,IID_IClassFactory,(void**)&pFactory);
	IMyObject * pMyObjectInterface;
	hr = pFactory->CreateInstance(NULL,_MyObjectInterfaceIID,(void**)&pMyObjectInterface);
	pFactory->Release();
	// .....
	// If Object no needed
	pMyObjectInterface->Release();
	// If Dll no needed 
	FreeLibrary(hDll); 
}
CoUninitialize(); // Shutdown COM for current thread


Regards,
Maxim.
 
Share this answer
 
Trying using uuid for your dlls and then loading them with correct uuid
 
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