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

本文介绍了如何将MFC ActiveX控件设置为安全的,以避免浏览器加载时出现警告。主要内容包括在工程中添加`Cathelp.h`和`Cathelp.cpp`文件,实现`CreateComponentCategory`和`RegisterCLSIDInCategory`函数,设置安全组件的GUID,并更新`DllRegisterServer`函数。同时,文章提到了在打包CAB文件时,需要将.lic文件与OCX插件一同打包并签名,以解决“不支持此对象属性”的错误。
摘要由CSDN通过智能技术生成

最近做了一个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后就可以正常加载了。


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值