ActiveX控件注册表安全描述及cab包发布注意事项

来源:互联网 发布:soul软件 编辑:程序博客网 时间:2024/05/19 04:02

最近做了一个ActiveX控件,要将控件描述和初始化为安全的,否则浏览器在加载控件时会出现警告信息。对于MFC ActiveX控件需要进行一下设置:

1.在工程中添加一下两个文件cathelp.h 和 cathelp.cpp来实现CreateComponentCategory , RegisterCLSIDInCategory两个帮助函数。

Cathelp.h

      #include "comcat.h"      // Helper function to create a component category and associated      // description      HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);      // Helper function to register a CLSID as belonging to a component      // category      HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

Cathelp.cpp

      #include "comcat.h"      // Helper function to create a component category and associated      // description      HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription)      {         ICatRegister* pcr = NULL ;         HRESULT hr = S_OK ;         hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,                               NULL,                               CLSCTX_INPROC_SERVER,                               IID_ICatRegister,                               (void**)&pcr);         if (FAILED(hr))            return hr;         // Make sure the HKCR\Component Categories\{..catid...}         // key is registered         CATEGORYINFO catinfo;         catinfo.catid = catid;         catinfo.lcid = 0x0409 ; // english         // Make sure the provided description is not too long.         // Only copy the first 127 characters if it is         int len = wcslen(catDescription);         if (len>127)            len = 127;         wcsncpy(catinfo.szDescription, catDescription, len);         // Make sure the description is null terminated         catinfo.szDescription[len] = '\0';         hr = pcr->RegisterCategories(1, &catinfo);         pcr->Release();         return hr;      }      // Helper function to register a CLSID as belonging to a component      // category      HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)      {         // Register your component categories information.         ICatRegister* pcr = NULL ;         HRESULT hr = S_OK ;         hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,                               NULL,                               CLSCTX_INPROC_SERVER,                               IID_ICatRegister,                               (void**)&pcr);         if (SUCCEEDED(hr))         {            // Register this category as being "implemented" by            // the class.            CATID rgcatid[1] ;            rgcatid[0] = catid;            hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);         }         if (pcr != NULL)            pcr->Release();         return hr;      }
2.在工程中找到实现DllRegisterServer函数的.cpp文件,按照下列顺序修改此文件:

  •    在此文件中包含定义CreateComponentCategory,RegisterCLSIDInCategory的头文件    
    #include "CatHelp.h"

  • 定义与安全组件相关的GUID,
    const CATID CATID_SafeForScripting = {0x7dd95801,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};                                                                                   const CATID CATID_SafeForInitializing = {0x7dd95802,0x9882,0x11cf,{0x9f,0xa9,0x00,0xaa,0x00,0x6c,0x42,0xc4}};


  • 将DllRegisterServer函数改为如下代码


  • STDAPI DllRegisterServer(void)      {          AFX_MANAGE_STATE(_afxModuleAddrThis);          if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))  //_tlid的定义一般已经自动生成,形式为:const GUID CDECL BASED_CODE _tlid              return ResultFromScode(SELFREG_E_TYPELIB);          if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))              return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( CreateComponentCategory(                  CATID_SafeForScripting,                  L"Controls that are safely scriptable") ))                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( CreateComponentCategory(                  CATID_SafeForInitializing,                  L"Controls safely initializable from persistent data") ))                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( RegisterCLSIDInCategory(                  _ctlid, CATID_SafeForScripting) ))             //_ctlid 是此空间类型的id,定义形式为GUID clsid = {0x810d0c77, 0x33a, 0x4c39, {0x55, 0xee, 0x68, 0xcf, 0xe2, 0x20, 0x3, 0xc2}};                return ResultFromScode(SELFREG_E_CLASS);          if (FAILED( RegisterCLSIDInCategory(                  _ctlid, CATID_SafeForInitializing) ))                return ResultFromScode(SELFREG_E_CLASS);          return NOERROR;      }

在打包cab文件发布时遇到了另外一个问题:

将inf文件及ocx插件打成cab包并签名后正常发布,也可以通过浏览器加载此控件,并且此时查看注册表的设置与直接在vs工程中安装后是一样的。但在调用ocx中的接口时一直显示“不支持此对象属性”之类的错误,最后经过多次测试将与工程中生成的与ocx文件同名的.lic文件一起打包cab后就可以正常加载了。


希望以上内容对大家有帮助。


原创粉丝点击