下面将分别介绍在MFC ActiveX和ATL中如何标记一个控件为安全的控件。
要标记一个MFC ActiveX控件为安全,可以仿照下面代码修改而得:
// CardScan.cpp : CCardScanApp 和DLL 注册的实现。 #include "stdafx.h" #include "CardScan.h" #include "comcat.h" #include "strsafe.h" #include "objsafe.h" CCardScanApp theApp; const GUID CDECL BASED_CODE _tlid = { 0x29959268, 0x9729, 0x458E, { 0xA8, 0x39, 0xBB, 0x39, 0x2E, 0xCB, 0x7E, 0x37 } }; const WORD _wVerMajor = 1; const WORD _wVerMinor = 0; const CATID CLSID_SafeItem = {0xB548F3C7,0x2135,0x4242,{0x92,0x0B,0xA7,0xBD,0xEE,0x6D,0x2B,0xA3}}; //{ 0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}}; // CCardScanApp::InitInstance - DLL 初始化 BOOL CCardScanApp::InitInstance() { BOOL bInit = COleControlModule::InitInstance(); if (bInit) { } return bInit; } // CCardScanApp::ExitInstance - DLL 终止 int CCardScanApp::ExitInstance() { return COleControlModule::ExitInstance(); } HRESULT CreateComponentCategory(CATID catid, CHAR *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 size_t len; // Make sure the provided description is not too long. // Only copy the first 127 characters if it is. // The second parameter of StringCchLength is the maximum // number of characters that may be read into catDescription. // There must be room for a NULL-terminator. The third parameter // contains the number of characters excluding the NULL-terminator. hr = StringCchLength(catDescription, STRSAFE_MAX_CCH, &len); if (SUCCEEDED(hr)) { if (len>127) { len = 127; } } else { // TODO: Write an error handler; } // The second parameter of StringCchCopy is 128 because you need // room for a NULL-terminator. hr = StringCchCopy(COLE2T(catinfo.szDescription), len + 1, catDescription); // Make sure the description is null terminated. catinfo.szDescription[len + 1] = '/0'; hr = pcr->RegisterCategories(1, &catinfo); pcr->Release(); return hr; } // HRESULT RegisterCLSIDInCategory - // Register your component categories information 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; } // HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) { ICatRegister *pcr = NULL ; HRESULT hr = S_OK ; hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr); if (SUCCEEDED(hr)) { // Unregister this category as being "implemented" by the class. CATID rgcatid[1] ; rgcatid[0] = catid; hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid); } if (pcr != NULL) pcr->Release(); return hr; } // DllRegisterServer - 将项添加到系统注册表 STDAPI DllRegisterServer(void) { HRESULT hr; AFX_MANAGE_STATE(_afxModuleAddrThis); if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid)) return ResultFromScode(SELFREG_E_TYPELIB); if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE)) return ResultFromScode(SELFREG_E_CLASS); // Mark the control as safe for initializing. hr = CreateComponentCategory(CATID_SafeForInitializing, _T("Controls safely initializable from persistent data!")); if (FAILED(hr)) return hr; hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); if (FAILED(hr)) return hr; // Mark the control as safe for scripting. hr = CreateComponentCategory(CATID_SafeForScripting, _T("Controls safely scriptable!")); if (FAILED(hr)) return hr; hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting); if (FAILED(hr)) return hr; return NOERROR; } // DllUnregisterServer - 将项从系统注册表中移除 STDAPI DllUnregisterServer(void) { HRESULT hr; AFX_MANAGE_STATE(_afxModuleAddrThis); // Remove entries from the registry. hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); if (FAILED(hr)) return hr; hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting); if (FAILED(hr)) return hr; if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor)) return ResultFromScode(SELFREG_E_TYPELIB); if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE)) return ResultFromScode(SELFREG_E_CLASS); return NOERROR; }这里值得注意的一个地方是DllUnregisterServer函数,在这段代码中,我是将
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);这两句代码放在
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor)) return ResultFromScode(SELFREG_E_TYPELIB); if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE)) return ResultFromScode(SELFREG_E_CLASS);这两句代码的前面,如果你查阅MSDN,将会发现它上面的顺序和我是相反的,这应该是微软的一个错误代码,如果按照MSDN的代码来写,则你使用regsvr32 -u CardScan.ocx反注册时会报下面的错误:
调整为我所说的顺序就没问题了。
2)要标记使用ATL写的ActiveX控件为安全的控件,这比MFC要简单的多,只需要在控件头文件中增加几行代码就可以了:
class ATL_NO_VTABLE CTestCtrl : … public IObjectSafetyImpl<CTestCtrl, INTERFACESAFE_FOR_UNTRUSTED_CALLER| INTERFACESAFE_FOR_UNTRUSTED_DATA>,然后在COM映射表中增加一项:
BEGIN_COM_MAP(CTestCtrl) … COM_INTERFACE_ENTRY(IObjectSafety) END_COM_MAP()Building a Signed ActiveX Control
ActiveX控件是个危险的东西,如果不对其合法性进行数字签名和验证,IE是会拒绝其安装的。
工具包准备:CABARC.exe, cert2spc.exe, makecab.exe, makecert.exe, signcode.exe(或新版本中的signtool),以上小工具都可以在VS的安装路径下"Common7"Tools"Bin找到,或去微软官方网站上下载。
ActiveX控件的安装过程中,一部分工作就是自注册,这需要控件在
VERSIONINFO
结构中定义
OLESelfRegister值,你可以对资源文件进行编辑如下BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "080403a8" BEGIN VALUE "CompanyName", "TODO: <公司名>" VALUE "FileDescription", "TODO: <文件说明>" VALUE "FileVersion", "1.0.0.1" VALUE "InternalName", "CardScan.ocx" VALUE "LegalCopyright", "TODO: (C) <公司名>。保留所有权利。" VALUE "OLESelfRegister", "/0" VALUE "OriginalFilename", "CardScan.ocx" VALUE "ProductName", "TODO: <产品名>" VALUE "ProductVersion", "1.0.0.1" END END BLOCK "VarFileInfo" BEGIN VALUE "Translation", 0x804, 936 END END打包为CAB文件
因为ActiveX控件要放在网站上供客户下载到本地,因此压缩是必需的。一段典型的html代码如下:
<OBJECT ID="FuckATL1" CODEBASE ="http://localhost:8080/CardScan.cab" CLASSID="CLSID:B548F3C7-2135-4242-920B-A7BDEE6D2BA3" WIDTH=300 HEIGHT=200 />CODEBASE就指明了要下载的压缩包,其中包含了oxc,dll控件等所需要的文件。
通常CAB文件包含了一个INF文件,它用来描述CAB文件的所有细节信息,下面举个简单例子,代码如下:
; Sample INF file for SCRIPTABLEACTIVEX.DLL [version] ; version signature (same for both NT and Win95) do not remove signature="$CHICAGO$" AdvancedINF=2.0 [Add.Code] CardScan.ocx=CardScan.ocx CardScan.inf=CardScan.inf [CardScan.ocx] file-win32-x86=thiscab clsid={B548F3C7-2135-4242-920B-A7BDEE6D2BA3} FileVersion=1,0,0,1 RegisterServer=yes [CardScan.inf] file=thiscab ; end of INF file版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://xingzhesun.blogbus.com/logs/53822346.html
看着xf的电脑中显示的网页,不停晃动的我可爱的ACTIVEX,我高兴起来。 整理开发activex并在网站上发布的控件过程如下。 @1本项目的开发工具是vs2005 @2本文参考了David Marcionek的“a comlete ActiveX Web control Tutorial”的文章http://www.codeproject.com/KB/COM/CompleteActiveX.aspx 一、控件的修改 1.创建ACTIVEX项目,可以使用MFC,可以使用ATL,在mfcActivex控件向导的第二步,“应用程序设置”页面不选"运行时许可证",因为我不想购买运行许可。 2.创建工程后,在打开rc文件进行编辑,在version结构中添加activex的自注册功能。 VALUE "OLESelfRegister", "/0" // Version //
3.编写在WEB页面中禁止提示的代码,主要是修改APP的注册函数,增加了两个函数 @HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription) @HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 修改了两个函数 @STDAPI DllRegisterServer(void) @HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 这里修要定义一个常量,该值为classid 在MyActivexCtrl.ccp和IDL文件中找到。 const CATID CLSID_SafeItem ={0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}}; 当我们自己开发的使用只需要讲CLSID_SafeItem的修改为我们自己控件的classID即可。 代码如下: #include "comcat.h" #include "strsafe.h" #include "objsafe.h"
const CATID CLSID_SafeItem ={0x36299202, 0x9ef, 0x4abf,{ 0xad, 0xb9, 0x47, 0xc5, 0x99, 0xdb, 0xe7, 0x78}}; HRESULT CreateComponentCategory(CATID catid, WCHAR *catDescription) { }
// HRESULT RegisterCLSIDInCategory - Register your component categories information
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 - Adds entries to the system registry
STDAPI DllRegisterServer(void) { HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid)) return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE)) return ResultFromScode(SELFREG_E_CLASS);
// Mark the control as safe for initializing.
hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!"); if (FAILED(hr)) return hr;
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); if (FAILED(hr)) return hr;
// Mark the control as safe for scripting.
hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!"); if (FAILED(hr)) return hr;
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting); if (FAILED(hr)) return hr;
return NOERROR; }
// HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) { ICatRegister *pcr = NULL ; HRESULT hr = S_OK ;
hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr); if (SUCCEEDED(hr)) { // Unregister this category as being "implemented" by the class. CATID rgcatid[1] ; rgcatid[0] = catid; hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid); }
if (pcr != NULL) pcr->Release();
return hr; }
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void) { HRESULT hr; // HResult used by Safety Functions
AFX_MANAGE_STATE(_afxModuleAddrThis);
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor)) return ResultFromScode(SELFREG_E_TYPELIB);
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE)) return ResultFromScode(SELFREG_E_CLASS);
// Remove entries from the registry.
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing); if (FAILED(hr)) return hr;
hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting); if (FAILED(hr)) return hr;
return NOERROR; } 4.修改编译连接的项目配置,配置属性->常规->项目默认值->MFC的使用=在静态库中使用MFC,这样的设置目的是为保证ACTIVEX能够在没有MFCdll的客户机上运行。 二、发布前的准备 1.使用MS Activex Control Pad创建html测试页。当然你也可以自己写html。测试成功。 2.快速创建一个网站。vs2005很方便的。在iis中设置默认网站的ip,主目录和主文件。 3.制作CAB文件。 因为客户端在打开包含activex控件的页面时,首先要下载该页面,然后自注册安装,再运行。ms要求将ACTIVEX控件depend的文件包为cab文件。 这里需要使用ms的cabarc.exe进行打包,并且写inf文件,在打包时需要将inf文件也加入。inf文件运行activex的安装。 在进行打包时使用David Marcionek写好的批处理文件即可,我们可以按照自己的需要进行修改。双击mkcal.bat文件创建myactivex.cab文件 4.修改测试的html文件加入codebase属性。 这里需要注意codebase属性指出了cab文件的具体位置,客户端从这位置下载cab文件,如果codebase的值不正确则造成,客户端不能够正确下载activex,David Marcionek在文章中给大家留下了这个bug。 <HEAD> <TITLE>MyActiveX CODEBASE</TITLE> </HEAD> <BODY> <center> MyActiveX With CODEBASE Example <p></p>
<OBJECT ID="MyActiveX1" WIDTH=350 HEIGHT=50 CODEBASE ="http://192.168.1.101/??/myactivex.cab" CLASSID="CLSID:36299202-09EF-4ABF-ADB9-47C599DBE778"> <PARAM NAME="_Version" VALUE="65536"> <PARAM NAME="_ExtentX" VALUE="2646"> <PARAM NAME="_ExtentY" VALUE="1323"> <PARAM NAME="_StockProps" VALUE="0"> </OBJECT>
</center> </BODY> </HTML>
5.将cab文件拷贝到网站的指定位置。 三、ie安全属性的设置。 又有我们没有购买数字证书,所以我们的控件是不安全的需要在IE中进行有关activex的设置 @activex控件自动提示=启用 @对已标记为安全的Activex控件进行初始化和脚本=启用 @对没有标记为安全的Activex控件进行初始化和脚本=提示 @二进制和脚本行为=启用 @下载未签名的ACTIVEX控件=提示 @下载已签名的ACTIVEX控件=提示 @运行ACTIVEX控件和插件=提示 重置为=安全级-中 我们这样设置首先保证了已签名的activex的正确下载安装和运行,同时对未签名的ACTIVEX控件都给与了“提示”,这样客户端的用户就可以根据自己的判断决定是否下载和安装ACTIVE.
四、在服务器端测试 1.首先将activex控件删除。因为vs在运行时已经注册了activex。我们的目的是通过网页自动下载安装activex。有些开发人员说在服务器activex运行正常,在客户机上不能正常运行。很可能是因为activex已经注册了。问题可能在web上。在本机上运行时应该出现提示是否下载cab文件。确定下载。运行正常。
五、在客户机上测试 1.首先保证客户机能正常访问你的web 2.在地址栏输入:http://192.168.1.101/index.htm 此时也提示你是否下载CAB文件。确定下载,你会在状态栏看到提示。 3.ie下载cab文件后根据inf文件安装文件,自注册,初始化,运行Active。 4.OK!
参考文献:
http://www.codeproject.com/KB/COM/CompleteActiveX.aspx
编写在浏览器中不弹出警告的ActiveX控件
http://www.vckbase.com/document/viewdoc/?id=728