默认,MFC ActiveX 控件不被标记为可安全编写脚本和可安全初始化。 该控件在 Internet Explorer 中运行该安全级别设置为中等或高时,这一明显。 在这两种模式任一,警告可能会显示控件的数据不安全,或者不在控件是可安全编写脚本来使用。
有一个控件可用于消除这些错误的两个方法。 第一个包括实现 IObjectSafety 接口的控件,并可用于更改它们的行为和成为"安全"如果在 Internet 浏览器的上下文中运行的控件。 第二个涉及修改控件的 DllRegisterServer 函数来标记在注册表中的"安全"控件。 本文介绍了这些方法的第二个。 第一个方法实现该 IObjectSafety 接口将介绍 Internet Client SDK。
请记住控件应该只被标记为安全是否,实际上,安全。 请参阅 Internet Client SDK 文档的这说明。 请参阅"安全初始化和脚本的 ActiveX 控件在组件开发部分下。
请注意 本文不包括如何将控件标记为安全的下载。 有关代码下载和代码签名的详细信息,请参阅 Internet Client SDK。
有一个控件可用于消除这些错误的两个方法。 第一个包括实现 IObjectSafety 接口的控件,并可用于更改它们的行为和成为"安全"如果在 Internet 浏览器的上下文中运行的控件。 第二个涉及修改控件的 DllRegisterServer 函数来标记在注册表中的"安全"控件。 本文介绍了这些方法的第二个。 第一个方法实现该 IObjectSafety 接口将介绍 Internet Client SDK。
请记住控件应该只被标记为安全是否,实际上,安全。 请参阅 Internet Client SDK 文档的这说明。 请参阅"安全初始化和脚本的 ActiveX 控件在组件开发部分下。
请注意 本文不包括如何将控件标记为安全的下载。 有关代码下载和代码签名的详细信息,请参阅 Internet Client SDK。
请按照下列步骤将您的 MFC ActiveX 控件标记为可安全编写脚本和可安全初始化:
- 通过将下列 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; }
- 请修改标记为可安全控件 DllRegisterServer。 在在项目中的一个.cpp 文件中找到 DllRegisterServer 的实现。 需要将多个操作添加到此.cpp 文件。 包含实现 CreateComponentCategory 和 RegisterCLSIDInCategory 文件:
#include "CatHelp.h"
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}};
const GUID CDECL BASED_CODE _ctlid = { 0x43bd9e45, 0x328f, 0x11d0, { 0xa6, 0xb9, 0x0, 0xaa, 0x0, 0xa7, 0xf, 0xc2 } };
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; }
- 您可能不希望删除组件类别,因为其他控件可能正在使用它。
- 尽管定义一个 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}