IE加載ocx時提示控件不安全的解決方法

使用MFC 開發的ocx的提示控件不安全的解決方法

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

#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(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
// 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;
}
// 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;
	
	
}

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

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 registry

STDAPI 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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章