远亲不如近邻,给自己一个机会,让更多人成为你的邻居,欢迎入住我家邻居社区 www.WoJiaLinJu.com

Activex控件在IE中也可以不显示安全提示

作者
88doc整理发布

简介:

Activex控件在IE中也可以不显示安全提示

//创建Component Categories中的初始化安全和脚本安全项  
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);  
//在CLSID中创建与Component Categories中初始化安全和脚本安全项中相对应的implemented Categories项  
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);  
//注销与CLSID中的相应implemented Categories项,一般用不到,因为其它程序可能也会用到这此项  
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);  
 
extern const GUID CDECL _tlid;  
extern const GUID CDECL CLSID_SafeItem;  
 
extern const WORD _wVerMajor;  
extern const WORD _wVerMinor;  
 
在对就的.cpp文件中加入以下内容,如果原来程序中已经有的,只改动其中的多余部分即可:  
 
//设置控件与注册表相关的类型库ID,后面跟有version(1.0)字样,从本工程的.idl文件中获取  
const GUID CDECL BASED_CODE _tlid =  
{ 0xB926A326, 0xBE91, 0x4337, { 0xA1, 0xDC, 0x76, 0x1B, 0x73, 0x15, 0x6B, 0x23 } };  
//控件在注册表中的CLSID,后面跟有control字样,从本工程的.idl文件中获取  
const GUID CDECL CLSID_SafeItem =  
{ 0xAF546E3F, 0xB5B8, 0x42D6, {0xBB, 0x74, 0x84, 0xB7, 0x25, 0xC0, 0x38, 0x4D}};  
 
const WORD _wVerMajor = 1;  
const WORD _wVerMinor = 0;  
 
STDAPI DllRegisterServer(void)  
{  
HRESULT hr;  
 
AFX_MANAGE_STATE(_afxModuleAddrThis);  
 
if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))  
return ResultFromScode(SELFREG_E_TYPELIB);  
 
if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))  
return ResultFromScode(SELFREG_E_CLASS);  
//创建脚本安全“补充”项,非CLSID中  
hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");  
if (FAILED(hr))  
return hr;  
//创建初始化安全“补充”项,非CLSID中  
hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!");  
if (FAILED(hr))  
return hr;  
//设置控件CLSID中补充项的脚本安全项,与“补充”项中的脚本安全项对应  
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);  
if (FAILED(hr))  
return hr;  
//设置控件CLSID中补充项的初始化安全项,与“补充”项中的初始化安全项对应  
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);  
if (FAILED(hr))  
return hr;  
 
return NOERROR;  
}  
 
   
 
// DllUnregisterServer - Removes entries from the system registry  
 
STDAPI DllUnregisterServer(void)  
{  
//HRESULT hr;  
AFX_MANAGE_STATE(_afxModuleAddrThis);  
 
if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))  
return ResultFromScode(SELFREG_E_TYPELIB);  
 
if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))  
return ResultFromScode(SELFREG_E_CLASS);  
 
//hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);  
//if (FAILED(hr))  
//return hr;  
//hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);  
//if (FAILED(hr))  
//return hr;  
 
return NOERROR;  
}  
 
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;  
}  
 
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 (FAILED(hr))  
return hr;  
}  
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[1] ;  
rgcatid[0] = catid;  
hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);  
}  
 
if (pcr != NULL)  
pcr->Release();  
 
return hr;  

//创建Component Categories中的初始化安全和脚本安全项
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);
//在CLSID中创建与Component Categories中初始化安全和脚本安全项中相对应的implemented Categories项
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
//注销与CLSID中的相应implemented Categories项,一般用不到,因为其它程序可能也会用到这此项
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

extern const GUID CDECL _tlid;
extern const GUID CDECL CLSID_SafeItem;

extern const WORD _wVerMajor;
extern const WORD _wVerMinor;

在对就的.cpp文件中加入以下内容,如果原来程序中已经有的,只改动其中的多余部分即可:

//设置控件与注册表相关的类型库ID,后面跟有version(1.0)字样,从本工程的.idl文件中获取
const GUID CDECL BASED_CODE _tlid =
{ 0xB926A326, 0xBE91, 0x4337, { 0xA1, 0xDC, 0x76, 0x1B, 0x73, 0x15, 0x6B, 0x23 } };
//控件在注册表中的CLSID,后面跟有control字样,从本工程的.idl文件中获取
const GUID CDECL CLSID_SafeItem =
{ 0xAF546E3F, 0xB5B8, 0x42D6, {0xBB, 0x74, 0x84, 0xB7, 0x25, 0xC0, 0x38, 0x4D}};

const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;

STDAPI DllRegisterServer(void)
{
HRESULT hr;

AFX_MANAGE_STATE(_afxModuleAddrThis);

if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
return ResultFromScode(SELFREG_E_TYPELIB);

if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
return ResultFromScode(SELFREG_E_CLASS);
//创建脚本安全“补充”项,非CLSID中
hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");
if (FAILED(hr))
return hr;
//创建初始化安全“补充”项,非CLSID中
hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!");
if (FAILED(hr))
return hr;
//设置控件CLSID中补充项的脚本安全项,与“补充”项中的脚本安全项对应
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
if (FAILED(hr))
return hr;
//设置控件CLSID中补充项的初始化安全项,与“补充”项中的初始化安全项对应
hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
if (FAILED(hr))
return hr;

return NOERROR;
}

 

// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
//HRESULT hr;
AFX_MANAGE_STATE(_afxModuleAddrThis);

if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
return ResultFromScode(SELFREG_E_TYPELIB);

if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
return ResultFromScode(SELFREG_E_CLASS);

//hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);
//if (FAILED(hr))
//return hr;
//hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);
//if (FAILED(hr))
//return hr;

return NOERROR;
}

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

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 (FAILED(hr))
return hr;
}
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[1] ;
rgcatid[0] = catid;
hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
}

if (pcr != NULL)
pcr->Release();

return hr;
}

 

另外
1 在HTML中写<object>标签时,把object的写法放在一个.js文件里,用document.write输出,这样不会提示激活!
源码复制打印
 //myactivex.js  
document.write("<object name='ActiveXName1' id='ActiveXName1' classid='................'>");  
...  
document.write("<param...");  //如果有参数的话  
...  
document.write("</object>");  
...  
 //myactivex.js
document.write("<object name='ActiveXName1' id='ActiveXName1' classid='................'>");
...
document.write("<param...");  //如果有参数的话
...
document.write("</object>");
...

 
2 获取事件可以用
源码复制打印
<SCRIPT LANGUAGE="JavaScript" for="ActiveXName1" EVENT="OnMyClick"> 
    alert(arguments.length);// arguments 这里是取参数  
</SCRIPT> 
<SCRIPT LANGUAGE="JavaScript" for="ActiveXName1" EVENT="OnMyClick">
    alert(arguments.length);// arguments 这里是取参数
</SCRIPT>

转csdn网友shepherds()
在classview下,向目录的第一个.h文件(即App启动头文件)中加入以下内容: