1. 新建一个解决方案,新建一个类库 ClassLibrary1
2.设置 ClassLibrary1 的属性 (1)应用程序 --> 程序集信息 --> 勾选 使程序集COM可见. (2)生成 --> 勾选 为COM Interop 注册.
3.修改程序集文件 AssemblyInfo.cs --> 添加代码 [assembly: AllowPartiallyTrustedCallers()]
4.添加接口类 IObjectSafety
[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); }
5.新建类库(或者用户控件...) 并且实现 IObjectSafety接口,然后添加自定义的方法属性等操作
[Guid("4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB")] public partial class UserControl1 : IObjectSafety { #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
//自定义方法 public string StartProcess(string path, string arg) { try { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = path; process.StartInfo.Arguments = arg; process.StartInfo.UseShellExecute = true; process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardInput = false; process.StartInfo.RedirectStandardOutput = false; process.Start(); } catch (Exception ex) { return ex.Message; } return string.Empty; } }
ActiveX组件制作成功,然后重新生成,会报无法注册类型库,不要理,不影响大局.
6.添加 --> 安装向导 --> Setup1 --> 点击下一步 --> 点击下一步 --> 选择 主输出 来自 [我们的类库名称] --> 下一步 --> 下一步 --> 完成.
7.添加卸载文件 --> 在c:\windows\ 中查找 msiexec.exe 复制到容易找得到的文件夹下
8.在安装向导的应用程序文件夹中右键 --> 添加 --> 文件 --> 选择刚才的 msiexec.exe -->可以重命名为卸载 --> 然后右键选择创建快捷方式 --> 把创建的快捷方式剪贴到用户的"程序"菜单中 --> 右键快捷方式 --> 选择属性 --> 找到Arguments --> 设置其属性为 : /X {项目编码} ( --> 项目编码为: 点击 安装部署 --> 选择属性窗口 --> 对应的 ProDuctCode的值)
以上为所以步奏,重新生成安装部署项目,会报错,不必在意,然后双击那个bin目录下的exe文件,安装文件
9.测试阶段,新建一个web项目 --> 在页面中 写入标签 -->
<object id="csharpActiveX" classid="clsid:4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB"></object>
如果为用户控件时,可以定义width和height,还可以定义codebase="xxx.exe"
在javascript中可以用id调用方法document.getElementById("csharpActiveX").StartProcess("calc.exe","");