Click here to Skip to main content
11,790,021 members (39,486 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ COM

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

using namespace SampleLib; // This is in the dll

SampleLib::ISampleLibPtr m_objDll; //The variable ISampleLibPtr is typedef 

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 5-Oct-12 4:27am
Edited 5-Oct-12 5:02am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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);
	// .....
	// If Object no needed
	// If Dll no needed 
CoUninitialize(); // Shutdown COM for current thread

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Trying using uuid for your dlls and then loading them with correct uuid

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

  Print Answers RSS
0 OriginalGriff 1,029
1 Maciej Los 790
2 KrunalRohit 686
3 CPallini 606
4 Richard MacCutchan 460

Advertise | Privacy | Mobile
Web01 | 2.8.1509028.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100