activex com 组件开发二 安全接口的实现
2014-09-27 15:01
288 查看
上篇文章介绍了 一个 控件的创建,但是控件嵌入ie 是不安全的行为 ,ie浏览器老弹出安全提示,这一节实现他的安全接口, 让ie 不弹出提示。
对于MFC写的ActiveX,通过修改注册表的方式来实现控件的安全性,微软说明http://support2.microsoft.com/kb/161873/en-us。下面做个简单的翻译:
1、在项目中创建两个文件 Cathelp.h
2 、Cathelp.cpp
3、在ActivexTest.cpp文件中,添加CLSID_SafeItem的定义
上面括号中的值 ActivexTestCrtl.cpp中获取到 如图
4、ActivexTest.cpp添加两个头文件:
5、ActivexTest.cpp中DllRegisterServer和DllUnregisterServer函数.
// DllRegisterServer - 将项添加到系统注册表
// 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;
}
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
AFX_MANAGE_STATE(_afxModuleAddrThis);
// 删除控件初始化安全入口.
HRESULT 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;
}
7、到这 安全接口就完成实现,ie就不会弹出提示。
对于MFC写的ActiveX,通过修改注册表的方式来实现控件的安全性,微软说明http://support2.microsoft.com/kb/161873/en-us。下面做个简单的翻译:
1、在项目中创建两个文件 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); // HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
2 、Cathelp.cpp
#include "stdafx.h" #include "comcat.h" #include "strsafe.h" #include "objsafe.h" // HRESULT CreateComponentCategory - Used to register ActiveX control as safe 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 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(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; }
3、在ActivexTest.cpp文件中,添加CLSID_SafeItem的定义
上面括号中的值 ActivexTestCrtl.cpp中获取到 如图
4、ActivexTest.cpp添加两个头文件:
#include "Cathelp.h" #include "objsafe.h"
5、ActivexTest.cpp中DllRegisterServer和DllUnregisterServer函数.
// DllRegisterServer - 将项添加到系统注册表
// 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;
}
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
AFX_MANAGE_STATE(_afxModuleAddrThis);
// 删除控件初始化安全入口.
HRESULT 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;
}
7、到这 安全接口就完成实现,ie就不会弹出提示。
相关文章推荐
- ATL开发的activex要放上网能调用方法要先实现安全接口
- 用Delphi为Asp开发文件上载组件的原理和具体实现过程
- PB7.0中实现Jaguar GTS组件开发
- SP 短信开发-PROVISION接口实现-反向取消实例代码
- ATL组件模拟之多接口实现和内部类实现多接口
- FleaPHP 开发指南 - 8. 如何使用 RBAC 组件实现访问控制
- SP 短信开发-PROVISION接口实现-反向取消实例代码
- COM 组件设计与应用(八)——实现多接口
- ASP.NET中启用Windows集成验证,怎样在调用System.DirectoryServices下的组件时传递安全上下文,也就是说当前用户凭据,来实现权限管理
- FleaPHP 开发指南 - 8. 如何使用 RBAC 组件实现访问控制
- SP 短信开发-PROVISION接口实现-正向订购实例代码
- J2EE Web服务开发系列之十三: 实现安全的AXIS Web服务,第2部分
- J2EE Web服务开发系列之十二: 实现安全的AXIS Web服务,第1部分
- J2EE Web服务开发系列之十二: 实现安全的AXIS Web服
- 今天在开发一个组件的时候遇了一点IHttpHandler接口的问题
- IExtenderProvider 接口的应用.实现自定义组件LilyValidateProvider
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用
- J2EE Web服务开发系列之十二: 实现安全的AXIS Web服务,第1部分
- FleaPHP 开发指南 - 8. 如何使用 RBAC 组件实现访问控制
- J2EE Web服务开发系列之十二: 实现安全的AXIS Web服务,第1部分