IE加载ocx时提示控件不安全的解决方法

来源:互联网 发布:佳能ip2880s清零软件 编辑:程序博客网 时间:2024/05/21 15:49

使用MFC 开发的ocx的提示控件不安全的解决方法

1.添加如下的文件在工程中

#include "comcat.h"// Helper function to create a component category and associated// descriptionHRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);// Helper function to register a CLSID as belonging to a component// categoryHRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

#include "StdAfx.h"#include "Cathelp.h"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// categoryHRESULT 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 registryHRESULT 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;}

第二 主文件中添加如下三个值

const CATID CLSID_SafeItem = {0x1f64433, 0x4c9d, 0x4365, {0x83, 0x46, 0x66, 0xae, 0x4f, 0xff, 0x51, 0xa}};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}};

注意:CLSID_SafeItem的值使用CTL主文件中的IMPLEMENT_OLECREATE_EX的宏值替换而来。格式一定要变,不然不管用。


第三 修改如下两个函数如下:

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);  return NOERROR;*///NEWÏÂÃæÊÇеÄ×¢²á´úÂë      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(  CLSID_SafeItem, CATID_SafeForScripting) ))  return ResultFromScode(SELFREG_E_CLASS);      if (FAILED( RegisterCLSIDInCategory(  CLSID_SafeItem, CATID_SafeForInitializing) ))  return ResultFromScode(SELFREG_E_CLASS);      return NOERROR;}/////////////////////////////////////////////////////////////////////////////// DllUnregisterServer - Removes entries from the system registrySTDAPI DllUnregisterServer(void){    /*AFX_MANAGE_STATE(_afxModuleAddrThis);    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor)) return ResultFromScode(SELFREG_E_TYPELIB);  if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))  return ResultFromScode(SELFREG_E_CLASS);  return NOERROR;*///NEWÏÂÃæÊÇеĴúÂë  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; }

好了,没了  ok了


 可以参考:

https://support.microsoft.com/zh-cn/kb/161873

http://www.cnblogs.com/lidabo/p/3605495.html

http://blog.csdn.net/u012702039/article/details/43148785

http://www.cnblogs.com/phinecos/archive/2008/08/07/1263270.html





0 0