Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ COM
Hi,
 
Here is my code
#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 5-Oct-12 4:27am
Edited 5-Oct-12 5:02am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
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