动态导入com dll [英] importing com dll dynmaically

查看:113
本文介绍了动态导入com dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码

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();
}


上面的代码可以正常工作.
我在这里在代码本身中导入dll.
但是我的情况是,有许多实现相同接口的不同版本的com dll.因此,基于给定的版本,我必须选择相关的com dll并进行导入,然后我必须调用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?

推荐答案

你好,

您必须知道要使用的对象的COM接口,或者至少知道您所需要的一种或几种方法.
您可以动态加载的COM dll,并且不需要注册该dll.

这是代码示例,希望这就是您所要表达的意思:

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



问候,
Maxim.



Regards,
Maxim.


尝试为您的dll使用uuid,然后使用正确的uuid加载它们
Trying using uuid for your dlls and then loading them with correct uuid


这篇关于动态导入com dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆