赞
踩
前提条件:COM是VSC++编写的,先引用头文件
#include "ATLXXXProject_i.h"
很多时候,我们调用COM组件都是先注册(regsvr32 xxx.dll),然后直接调用COM接口就行了,比如:
- //先声明
- CComPtr<IATLXXXObject> m_spXXXObject;
-
- //再调用
- CoInitialize(nullptr);
- HRESULT hr = m_spXXXObject.CoCreateInstance(CLSID_ATLXXXObject);
- if (SUCCEEDED(hr)) {
- m_spXXXObject->CallMethod();
- }
那如果这个dll更新特频繁,每次都需要反注册,再重新注册,似乎有那么一点点的烦人哦。既然COM组件也是一种库文件,那有没有一种调用方法,像调用动态链接库那样先加载,再寻址,最后调用呢?方法肯定是有的。请看示例:
- HINSTANCE hDllInstance = LoadLibrary(_T("ATLXXXProject.dll"));
- if (hDllInstance == NULL) {
- // 处理加载DLL失败的情况
- return ;
- }
-
- typedef HRESULT(CALLBACK* LPFNDLLGETCLASSOBJECT)(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
- LPFNDLLGETCLASSOBJECT pfnDllGetClassObject;
-
- pfnDllGetClassObject = (LPFNDLLGETCLASSOBJECT)GetProcAddress(hDllInstance, "DllGetClassObject");
- if (pfnDllGetClassObject == NULL) {
- // 处理获取函数地址失败的情况
- FreeLibrary(hDllInstance);
- return ;
- }
-
- IClassFactory* pClassFactory;
- HRESULT hr = pfnDllGetClassObject(CLSID_ATLXXXObject, IID_IClassFactory, (LPVOID*)&pClassFactory);
- if (FAILED(hr)) {
- // 处理获取Class Factory失败的情况
- FreeLibrary(hDllInstance);
- return ;
- }
-
- IUnknown* pUnk;
- hr = pClassFactory->CreateInstance(NULL, IID_IUnknown, (LPVOID*)&pUnk);
- pClassFactory->Release(); // 创建完成后释放 Class Factory
- if (FAILED(hr)) {
- // 处理创建接口实例失败的情况
- FreeLibrary(hDllInstance);
- return ;
- }
-
- IATLXXXObject* pInterface;
- hr = pUnk->QueryInterface(IID_IATLXXXObject, (LPVOID*)&pInterface);
- pUnk->Release(); // QueryInterface 完成后释放 IUnknown
- if (FAILED(hr)) {
- // 处理查询接口失败的情况
- FreeLibrary(hDllInstance);
- return ;
- }
-
- pInterface->CallMethod();
-
- // 释放接口
- pInterface->Release();
- // 释放资源
- FreeLibrary(hDllInstance);
代码虽然复杂很多,但是目的我们是达到了,庆祝一下吧!(^-^)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。