一、前言
????? 最近有這樣一個需求,需要在網頁上面啟動客戶端的軟件,軟件之間的通信、調用,單單依靠HTML是無法實現了,因此必須借用Activex來實現。由于本人主要擅長C#,自然本文給出了用C#實現的范例,本文的預期效果是有一定Winform基礎的人可都輕松讀懂本文。



using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;namespace EasyActivex {[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")][InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]public interface IObjectSafety{[PreserveSig]int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);[PreserveSig()]int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);} }
?



using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace EasyActivex {//這個Guid,網頁調用的時候用到,Mark[Guid("685F0A47-944D-4145-BF4E-76A02A422B02")]//這里要實現IObjectSafety接口public partial class EUserControl : UserControl, IObjectSafety {public EUserControl(){InitializeComponent();}#region IObjectSafety 接口成員實現(直接拷貝即可)private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;private const int S_OK = 0;private const int E_FAIL = unchecked((int)0x80004005);private const int E_NOINTERFACE = unchecked((int)0x80004002);private bool _fSafeForScripting = true;private bool _fSafeForInitializing = true;public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions){int Rslt = E_FAIL;string strGUID = riid.ToString("B");pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;switch (strGUID){case _IID_IDispatch:case _IID_IDispatchEx:Rslt = S_OK;pdwEnabledOptions = 0;if (_fSafeForScripting == true)pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;break;case _IID_IPersistStorage:case _IID_IPersistStream:case _IID_IPersistPropertyBag:Rslt = S_OK;pdwEnabledOptions = 0;if (_fSafeForInitializing == true)pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;break;default:Rslt = E_NOINTERFACE;break;}return Rslt;}public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions){int Rslt = E_FAIL;string strGUID = riid.ToString("B");switch (strGUID){case _IID_IDispatch:case _IID_IDispatchEx:if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))Rslt = S_OK;break;case _IID_IPersistStorage:case _IID_IPersistStream:case _IID_IPersistPropertyBag:if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))Rslt = S_OK;break;default:Rslt = E_NOINTERFACE;break;}return Rslt;}#endregion/// <summary>/// 打開記事本/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnOpenNote_Click(object sender, EventArgs e){System.Diagnostics.Process.Start("notepad.exe"); }} }
小提示:EUserControl代碼的Guid可以用VS附帶的Guid生成工具生成:

//用戶添加 [assembly: AllowPartiallyTrustedCallers()]
?




[version] signature="$CHICAGO$" AdvancedINF=2.0 [Setup Hooks] hook1=hook1 [hook1] run=msiexec.exe /i "%EXTRACT_DIR%\EasySetup.msi" /qn
?
2)?? build.bat制作。新建txt文件,加入以下內容,將文件名重新命名build.bat即可。其中EasyActivex.cab是生成目標cab的名稱;install.inf是第一步生成的文件名,而EasySetup.msi是需要打包的安裝程序名;第二條ping命令僅僅是讓批處理不要那么快退出,起到更利于觀察生成結果的作用。
"cabarc.exe" -s 6144 n EasyActivex.cab install.inf EasySetup.msi ping -n 20 127.0.0.1 >nul
?




<object id="csharpActiveX" codebase="Activex/EasyActivex.cab" classid="clsid:685F0A47-944D-4145-BF4E-76A02A422B02"></object>
運行效果如下:
點擊即可在網頁中打開記事本了。
?
五、本案例源碼+cab打包工具+數字簽名工具下載
?
六、參考資料:
1、使用C#開發ActiveX控件 ?http://www.cnblogs.com/yilin/archive/2009/09/15/1567332.html?
2、Activex簽名方法和工具技巧??http://www.360doc.com/content/10/0901/15/203871_50402416.shtml
3、中國數字認證網用戶手冊?http://www.ca365.com/forward.do?pageurl=/ca/yhsc.jsp
?
?