不注册COM组件直接调用接口

来源:互联网 发布:淘宝排名权重 编辑:程序博客网 时间:2024/04/30 08:47

 本文以COM组件AppLogo.dll为例,AppLogo.dll中提供了IDunRui接口,在不使用regsvr32向系统注册的情况下创建IDunRui接口并调用。

一、导入组件或类型库;

    在C++中使用COM组件,需要向其提供类型库,以公开接口和方法。类型库文件类型为tlb文件,直接#import导入即可。类型库也可以以资源形式编译进COM组件Dll内部,资源类型为TYPELIB,资源号为1即可。如果Dll中已包含类型库,只需#import导入COM组件Dll文件即可。代码如下:

复制代码
#import "AppLogo.dll" no_namespace// or...#import "AppLogo.tlb" no_namespace// or...#import "AppLogo.dll"using namespace AppLogo;// or...#import "AppLogo.tlb" using namespace AppLogo;
复制代码

二、动态载入COM组件并创建接口。

复制代码
// Declare in class as a member.CDllFromFile m_dllCOM;// Load Dll file.m_dllCOM.LoadLibrary(_T("AppLogo.dll"));//TODO in initializing.IDunRui *pDunrui = NULL;CComQIPtr<IClassFactory> pClassFactory;typedef HRESULT (__stdcall *fnDllGetClassObject)(REFCLSID rclsid, REFIID riid, LPVOID *ppv);fnDllGetClassObject pDllGetClassObject = (fnDllGetClassObject)m_dllCom.GetProcAddress("DllGetClassObject");if (NULL != pDllGetClassObject)   {      HRESULT hr = pDllGetClassObject(__uuidof(AppLogo::DunRui), IID_IClassFactory, (LPVOID*)&pClassFactory);      if (S_OK == hr)      {          hr = pClassFactory->CreateInstance(NULL, __uuidof(AppLogo::IDunRui), (LPVOID*)&pDunrui);          if (S_OK == hr)          {                AfxMessageBox(_T("Succeed !"));          }      } }
复制代码

CDllFromFile源码下载:http://url.cn/L39MWg


0 0
原创粉丝点击