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

之前编写的ocx控件在ie浏览器加载的时候,会出现一个提示。

IE加载ocx时提示控件不安全的解决方法_第1张图片

这是因为ocx控件有一个ID,而这个ID注册后IE并没有认为它是安全的控件。

我们必须把这个控件注册为安全控件。



新建的mfc Activity项目有三个重要的类,xxApp,xxCtrl,xxPropPage三个类。

1.在xxApp类头文件添加如下函数声明:

HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);
// Helper function to register a CLSID as belonging to a component
// category
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
// Helper function to unregister a CLSID as belonging to a component
// category
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

在xxApp类的实现文件添加如下:

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;


}


2.注册GUID

在xxApp.cpp定义两个GUID用来注册控件安全

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}};


同时在xxApp.cpp文件定义安全的CLSID

const CATID CLSID_SafeItem = {0x3ce7a2fd, 0x1ae9, 0x40b0, 0x83, 0xdb, 0x4a, 0x32, 0xce, 0xb2, 0xcf, 0x30};


const GUID CDECL _tlid = { 0x59A67716, 0x53F0, 0x464A, { 0x9D, 0x35, 0x30, 0x24, 0x68, 0xBF, 0x1F, 0xCC } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

注意:CLSID_SafeItem其实就是xxCtrl.cpp里的这个值。

// 初始化类工厂和 guid

IMPLEMENT_OLECREATE_EX(CActivePZCtrl, "ACTIVEPZ.ActivePZCtrl.1",
	0x3ce7a2fd, 0x1ae9, 0x40b0, 0x83, 0xdb, 0x4a, 0x32, 0xce, 0xb2, 0xcf, 0x30)

3.修改注册和反注册的代码

在xxApp.cpp文件的DllRegisterServer函数和DllUnRegisterServer函数。

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 - 将项从系统注册表中移除

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;
}

4.这个提示解决之后,因为IE设置的问题,还会出现如下提示:



可以在internet选项中设置

IE加载ocx时提示控件不安全的解决方法_第2张图片

IE加载ocx时提示控件不安全的解决方法_第3张图片

链接:ActiveX控件开发的一些问题一,如何标记控件为安全

浏览器插件之ActiveX开发(一)


你可能感兴趣的:(工作中遇到的问题)