本文介绍了动态导入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?
推荐答案
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.
这篇关于动态导入com dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!