发表于2014-07-07 21:17:40
开发环境:Win7、VS2010
1. 创建类
2. 设置工程属性
2.1. 在应用程序页中,打开 程序集信息 ,勾选 使程序集COM可见
2.2. 在 生成 页中, 勾选 为COM互操作注册
2.3. 在 Properties.AssemblyInfo.cs文件中 添加 `[assembly:AllowPartiallyTrustedCallers()]`(注意引入:`System.Security`命名空间)
3. 添加用户控件
ActiveX以用户控件为载体,加载到网页中
4. 添加控件的GUID
ActiveX的用户控件均有一个独立的GUID标识,该GUID必须与工程的GUID不同。
4.1. 通过VS2010->工具->创建GUID
4.2. 引入`System.Runtime.InteropServices`
4.3. 将生成的GUID粘贴到用户控件类声明前
[Guid("4D39585B-7947-4197-8BDB-B0A6918B1098")] public partial class ATC : UserControl { InitializeComponent(); }
5. 开发IObjectSafety接口
为了让ActiveX控件获得客户端的信任,用户控件必须实现`IObjectSafety`接口,并且下面的代码是固定的(GUID也不能变)
[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); }
6. 用户控件实现IObjectSafety接口
[Guid("4D39585B-7947-4197-8BDB-B0A6918B1098")] public partial class ATC : UserControl, IObjectSafety { 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
7. 获取ActiveX的Classid
7.1. 打开VS2010->工具->OleView(若没有就自行添加,程序路径:C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\OleView.exe)。
7.2. 在`Object Classes`->`Grouped by Component Category`->`.NET Category`找到刚才新建的ActiveX控件
7.3. 右键复制HTML标签
8. 页面引用ActiveX控件
在html页面上
<object classid="clsid:ActiveX控件的clsid" codebase="控件打包后的exe文件名或cab文件名" width="200px" height="200px"> </object>
作者:^_^肥仔John
原文地址:http://www.cnblogs.com/fsjohnhuang/p/3823102.html