如何将标记为可安全编写脚本和初始化的 MFC ActiveX 控件

默认,MFC ActiveX 控件不被标记为可安全编写脚本和可安全初始化。 该控件在 Internet Explorer 中运行该安全级别设置为中等或高时,这一明显。 在这两种模式任一,警告可能会显示控件的数据不安全,或者不在控件是可安全编写脚本来使用。

有一个控件可用于消除这些错误的两个方法。 第一个包括实现 IObjectSafety 接口的控件,并可用于更改它们的行为和成为"安全"如果在 Internet 浏览器的上下文中运行的控件。 第二个涉及修改控件的 DllRegisterServer 函数来标记在注册表中的"安全"控件。 本文介绍了这些方法的第二个。 第一个方法实现该 IObjectSafety 接口将介绍 Internet Client SDK。

请记住控件应该只被标记为安全是否,实际上,安全。 请参阅 Internet Client SDK 文档的这说明。 请参阅"安全初始化和脚本的 ActiveX 控件在组件开发部分下。

请注意 本文不包括如何将控件标记为安全的下载。 有关代码下载和代码签名的详细信息,请参阅 Internet Client SDK。
请按照下列步骤将您的 MFC ActiveX 控件标记为可安全编写脚本和可安全初始化:
  1. 通过将下列 cathelp.h 和 cathelp.cpp 文件添加到项目中实现,CreateComponentCategory 和 RegisterCLSIDInCategory 帮助器函数。

    Cathelp.h

    <script type="text/javascript"></script>
          #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

    <script type="text/javascript"></script>
          #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 文件中找到 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}};
    						
    定义与控件关联的 GUID。 为简单起见,您可以借用从控件的主要的.cpp 文件中的 IMPLEMENT_OLECREATE_EX 宏中 GUID。 略有调整格式,使其如以下所示:
          const GUID CDECL BASED_CODE _ctlid =
          { 0x43bd9e45, 0x328f, 0x11d0,
                  { 0xa6, 0xb9, 0x0, 0xaa, 0x0, 0xa7, 0xf, 0xc2 } };
    						
    的脚本和初始化,修改 DllRegisterServer 函数,如下所示,将控件标记为两个安全:
          STDAPI DllRegisterServer(void)
          {
              AFX_MANAGE_STATE(_afxModuleAddrThis);
    
              if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _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) ))
                    return ResultFromScode(SELFREG_E_CLASS);
    
              if (FAILED( RegisterCLSIDInCategory(
                      _ctlid, CATID_SafeForInitializing) ))
                    return ResultFromScode(SELFREG_E_CLASS);
    
              return NOERROR;
          }
    						
您将不正常情况下修改 DllUnregisterServer 函数有以下两个原因:
  • 您可能不希望删除组件类别,因为其他控件可能正在使用它。
  • 尽管定义一个 UnRegisterCLSIDInCategory 函数默认情况下 DllUnregisterServer 删除控制的项在注册表完全。 因此,从控件的注册中删除该类别是很少使用。
编译并注册控件后, 您可以找到以下项在注册表中:
   HKEY_CLASSES_ROOT/Component
   Categories/{7DD95801-9882-11CF-9FA9-00AA006C42C4}

   HKEY_CLASSES_ROOT/Component
   Categories/{7DD95802-9882-11CF-9FA9-00AA006C42C4}

   HKEY_CLASSES_ROOT/CLSID/{"your controls GUID"}/Implemented
   Categories/{7DD95801-9882-11CF-9FA9-00AA006C42C4}

   HKEY_CLASSES_ROOT/CLSID/{"your controls GUID"}/Implemented
   Categories/{7DD95802-9882-11CF-9FA9-00AA006C42C4}
				
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值