Client namespace wuhong.Client { class Program { static void Main(string[] args) { if (args != null && args.Length > 0) { Array.ForEach(args, arg => Console.WriteLine(arg)); } Console.ReadLine(); } } }
注册表 [HKEY_CLASSES_ROOT\wuhong.client] @="wuhong.Client" "URL Protocol"="" [HKEY_CLASSES_ROOT\wuhong.client\DefaultIcon] @=" wuhong.Client.exe " [HKEY_CLASSES_ROOT\wuhong.client\Shell] [HKEY_CLASSES_ROOT\wuhong.client\Shell\open] [HKEY_CLASSES_ROOT\wuhong.client\Shell\open\command] @="\"[TARGETDIR]wuhong.Client.exe\" \"%1\""
IobjectSafety接口 namespace wuhong.ActiveX { [ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")] [InterfaceType(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); } }
接下来是控件的代码,实现一个“wuhong.client”的控件,本身不添加任何额外的功能。
UserControl类 namespace wuhong.ActiveX { [Guid("9C9701D1-D188-495d-8721-9D246211A27C"), ProgId("wuhong.client"), ComVisible(true)] public partial class ActiveXObject : UserControl, IObjectSafety { public ActiveXObject() { InitializeComponent(); } private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) { pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; return S_OK; } public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { return S_OK; } } }
另外AssemblyInfo.cs中需要修改并添加以下内容:
using System.Security; [assembly: AllowPartiallyTrustedCallers()] [assembly: ComVisible(true)]
项目生成中也需要选择“为COM互操作注册”。
Test页面 <html xmlns="http://www.w3.org/1999/xhtml" > <head> <script type=‘text/javascript‘> function Start() { try {//支持 var obj = new ActiveXObject("wuhong.client"); } catch (e) {//不支持 } if(null != obj){ delete obj; window.navigate(‘wuhong.client:start?HelloWorld‘); } else{ alert("您未安装程序,请安装!"); } } </script> <title></title> </head> <body> <input type="button" onclick="Start()" value="启动"/> </body> </html>
原文:http://www.cnblogs.com/luwenbin/p/3516730.html