去掉IE提示:在此页上的ActiveX控件和本页上的其他部分的交互可能不安全。你想允许这种交互吗?

时间:2021-05-04 14:26:45

由于项目需求,需要用到OCX控件。而在IE浏览器中加载OCX控件会有如下提示:

去掉IE提示:在此页上的ActiveX控件和本页上的其他部分的交互可能不安全。你想允许这种交互吗?

这是因为OCX控件有一个ID,而这个ID注册后IE不认为该OCX控件是安全的。所以,必须把这个控件注册为安全控件。

假设已经创建好OCX控件工程了。操作步骤如下:

第一步:在xxxApp类头文件(.h)添加如下函数声明。

// 去掉IE提示“此控件不安全的”提示框;
// 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);
// Helper function to unregister a CLSID as belonging to a component
// category
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

注意:这些函数声明不是xxxApp类成员函数声明,而是全局函数声明。

第二步:在xxxApp类的实现文件(.cpp)添加如下函数实现。

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 > )
len = ;
wcsncpy(catinfo.szDescription, catDescription, len);
// Make sure the description is null terminated
catinfo.szDescription[len] = '/0';
hr = pcr->RegisterCategories(, &catinfo);
pcr->Release();
return hr;
} 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[];
rgcatid[] = catid;
hr = pcr->RegisterClassImplCategories(clsid, , rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
} 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[];
rgcatid[] = catid;
hr = pcr->UnRegisterClassImplCategories(clsid, , rgcatid);
}
if (pcr != NULL)
pcr->Release();
return hr;
}

第三步:在xxxApp类的实现文件(xxx.cpp)定义两个GUID用来注册控件的安全性,同时需要定义要注册为安全的CLSID。

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 } }; const CATID CLSID_SafeItem = { 0xedb3ce, 0xe6b9, 0x4883, { 0x83, 0xc0, 0x3f, 0xbb, 0x40, 0xf7, 0x58, 0x5f } };

两个GUID为CATID_SafeForScripting 、CATID_SafeForInitializing;安全的CLSID为CLSID_SafeItem。

在OCX控件注册完成后,在注册表的如下地方可以看到上面的两个GUID。

  1)32位版本位置:

    HKEY_CLASSES_ROOT\Wow6432Node\CLSID\[xxxCtrl类的uuid]\Implemented Categories,下面有这两个GUID。

  2)64位版本位置:

    HKEY_CLASSES_ROOT\CLSID\[xxxCtrl类的uuid]\Implemented Categories,下面有这两个GUID。

  注意:[xxxCtrl类的uuid]是xxx.idl文件中xxxCtrl的类信息的uuid。

CLSID_SafeItem的值是根据在xxxCtrl类实现文件(xxxCtrl.cpp)中IMPLEMENT_OLECREATE_EX的定义而来的(实际上就是ActiveX的CLASSID)。IMPLEMENT_OLECREATE_EX值如下:

去掉IE提示:在此页上的ActiveX控件和本页上的其他部分的交互可能不安全。你想允许这种交互吗?

第四步:修改OCX控件的注册、反注册代码。

// DllRegisterServer - 将项添加到系统注册表
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; //新注册代码
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; //新反注册代码
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;
}

至此完成了注册安全性的OCX控件。