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

转csdn网友shepherds()

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

  1. //创建Component Categories中的初始化安全和脚本安全项   
  2. HRESULT  CreateComponentCategory(CATID catid,  WCHAR * catDescription);  
  3. //在CLSID中创建与Component Categories中初始化安全和脚本安全项中相对应的implemented Categories项   
  4. HRESULT  RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);  
  5. //注销与CLSID中的相应implemented Categories项,一般用不到,因为其它程序可能也会用到这此项   
  6. HRESULT  UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);  
  7.   
  8. extern   const  GUID CDECL _tlid;  
  9. extern   const  GUID CDECL CLSID_SafeItem;  
  10.   
  11. extern   const   WORD  _wVerMajor;  
  12. extern   const   WORD  _wVerMinor;  
  13.   
  14. 在对就的.cpp文件中加入以下内容,如果原来程序中已经有的,只改动其中的多余部分即可:  
  15.   
  16. //设置控件与注册表相关的类型库ID,后面跟有version(1.0)字样,从本工程的.idl文件中获取   
  17. const  GUID CDECL BASED_CODE _tlid =  
  18. { 0xB926A326, 0xBE91, 0x4337, { 0xA1, 0xDC, 0x76, 0x1B, 0x73, 0x15, 0x6B, 0x23 } };  
  19. //控件在注册表中的CLSID,后面跟有control字样,从本工程的.idl文件中获取   
  20. const  GUID CDECL CLSID_SafeItem =  
  21. { 0xAF546E3F, 0xB5B8, 0x42D6, {0xBB, 0x74, 0x84, 0xB7, 0x25, 0xC0, 0x38, 0x4D}};  
  22.   
  23. const   WORD  _wVerMajor = 1;  
  24. const   WORD  _wVerMinor = 0;  
  25.   
  26. STDAPI DllRegisterServer(void )  
  27. {  
  28. HRESULT  hr;  
  29.   
  30. AFX_MANAGE_STATE(_afxModuleAddrThis);  
  31.   
  32. if  (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))  
  33. return  ResultFromScode(SELFREG_E_TYPELIB);  
  34.   
  35. if  (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))  
  36. return  ResultFromScode(SELFREG_E_CLASS);  
  37. //创建脚本安全“补充”项,非CLSID中   
  38. hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!" );  
  39. if  (FAILED(hr))  
  40. return  hr;  
  41. //创建初始化安全“补充”项,非CLSID中   
  42. hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!" );  
  43. if  (FAILED(hr))  
  44. return  hr;  
  45. //设置控件CLSID中补充项的脚本安全项,与“补充”项中的脚本安全项对应   
  46. hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);  
  47. if  (FAILED(hr))  
  48. return  hr;  
  49. //设置控件CLSID中补充项的初始化安全项,与“补充”项中的初始化安全项对应   
  50. hr = RegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);  
  51. if  (FAILED(hr))  
  52. return  hr;  
  53.   
  54. return  NOERROR;  
  55. }  
  56.   
  57.    
  58.   
  59. // DllUnregisterServer - Removes entries from the system registry   
  60.   
  61. STDAPI DllUnregisterServer(void )  
  62. {  
  63. //HRESULT hr;   
  64. AFX_MANAGE_STATE(_afxModuleAddrThis);  
  65.   
  66. if  (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))  
  67. return  ResultFromScode(SELFREG_E_TYPELIB);  
  68.   
  69. if  (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))  
  70. return  ResultFromScode(SELFREG_E_CLASS);  
  71.   
  72. //hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);   
  73. //if (FAILED(hr))   
  74. //return hr;   
  75. //hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);   
  76. //if (FAILED(hr))   
  77. //return hr;   
  78.   
  79. return  NOERROR;  
  80. }  
  81.   
  82. HRESULT  CreateComponentCategory(CATID catid,  WCHAR * catDescription)  
  83. {  
  84. ICatRegister* pcr = NULL ;  
  85. HRESULT  hr = S_OK ;  
  86.   
  87. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,   
  88. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void **)&pcr);  
  89. if  (FAILED(hr))  
  90. return  hr;  
  91.   
  92. // Make sure the HKCR\Component Categories\{..catid...}   
  93. // key is registered.   
  94. CATEGORYINFO catinfo;  
  95. catinfo.catid = catid;  
  96. catinfo.lcid = 0x0409 ; // english   
  97.   
  98. // Make sure the provided description is not too long.   
  99. // Only copy the first 127 characters if it is.   
  100. int  len = wcslen(catDescription);  
  101. if  (len>127)  
  102. len = 127;  
  103. wcsncpy(catinfo.szDescription, catDescription, len);  
  104. // Make sure the description is null terminated.   
  105. catinfo.szDescription[len] = '\0' ;  
  106.   
  107. hr = pcr->RegisterCategories(1, &catinfo);  
  108. pcr->Release();  
  109.   
  110. return  hr;  
  111. }  
  112.   
  113. HRESULT  RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)  
  114. {  
  115. // Register your component categories information.   
  116. ICatRegister* pcr = NULL ;  
  117. HRESULT  hr = S_OK ;  
  118. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,   
  119. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void **)&pcr);  
  120. if  (SUCCEEDED(hr))  
  121. {  
  122. // Register this category as being "implemented" by the class.   
  123. CATID rgcatid[1] ;  
  124. rgcatid[0] = catid;  
  125. hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);  
  126.   
  127. if  (FAILED(hr))  
  128. return  hr;  
  129. }  
  130. if  (pcr != NULL)  
  131. pcr->Release();  
  132. return  hr;  
  133. }  
  134.   
  135.   
  136. HRESULT  UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)  
  137. {  
  138. ICatRegister* pcr = NULL ;  
  139. HRESULT  hr = S_OK ;  
  140.   
  141. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,   
  142. NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void **)&pcr);  
  143. if  (SUCCEEDED(hr))  
  144. {  
  145. // Unregister this category as being "implemented" by the class.   
  146. CATID rgcatid[1] ;  
  147. rgcatid[0] = catid;  
  148. hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);  
  149. }  
  150.   
  151. if  (pcr != NULL)  
  152. pcr->Release();  
  153.   
  154. return  hr;  
  155. }  


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

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

猜你喜欢

转载自liuleijsjx.iteye.com/blog/766037
今日推荐