现在的位置: 首页 > 综合 > 正文

如何标记为安全 MFCActiveX 控件对于脚本和初始化

2012年08月19日 ⁄ 综合 ⁄ 共 4284字 ⁄ 字号 评论关闭
注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。
文章编号 : 161873
最后修改 : 2005年9月1日
修订 : 3.0

概要

默认情况下, MFC ActiveX 控件不标记, 为脚本安全和对初始化安全。 InternetExplorer 中运行该控件与设为中型或高安全级别时这将成为明显。 对这些模式, 中警告可能显示控件的数据是不安全或, 控件可能不安全对于要使用脚本。

有两种方法, 控件可用于消除这些错误。 第一个涉及控件实现 IObjectSafety 接口, 对于控件, 要更改其行为, 如果一个 Internet 浏览器的上下文中运行 " 安全 " 成为很有用。 第二涉及修改控件的 DllRegisterServer 函数来标记该控件与注册表中 " 安全 "。 本文介绍第二种方法。 Internet 客户端 SDK 中包含首方法, 实现 IObjectSafety 接口,。

请记住, 控件应只标记, 如果是实际上, 安全, 安全。 请参考 Internet 客户端 SDK 文档有关的此说明。 请组件开发节下参阅 " 安全初始化和对于 ActiveX 控件脚本 "。

注意 本文没有涉及如何标记用于下载安全控件。 有关代码下载和代码签名, 上详细信息请参阅以 Internet 客户端 SDK。

更多信息

请按照下列步骤来将 MFC ActiveX 控件标记为对于脚本安全和对初始化安全:

1. 通过向项目添加下列 cathelp.h 和 cathelp.cpp 文件实现 CreateComponentCategory 和 RegisterCLSIDInCategory Helper 函数。

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 来标记作为安全控件。 实现了 DllRegisterServer 定位项目中 .cpp 文件中。 将需要几种方法添加到此 .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。 为简便起见, 您可借用 GUID 从 IMPLEMENT_OLECREATE_EX 宏对控件主 .cpp 文件中。 略微调整格式: 以便它类似以下

      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}
				

参考

Internet 客户端 SDK - 组件开发 - 安全初始化和脚本对于 ActiveX 控件
//http://support.microsoft.com/kb/161873/zh-cn

抱歉!评论已关闭.