.Net魔法堂:史上最全的ActiveX开发教程——开发篇
一、刖百在设计某移动内部自动化运维平台时,经综合考虑终端机性能和功能需求等 因素后,决定采用B/S模式,并且浏览器通过ActiveX组件实现与服务器 Agent作P2P的通讯。好处,整个平台以网页形式存在,界面渲染性能高于桌 面应用(终端机性能其低);通过ActiveX组件与各服务器Agent进行P2P 通讯,不对Web服务器造成压力。风险,当用ActiveX传输上百兆的文件 时,会对浏览器造成哪些影响;团队中没有类似解决方案的经验供借鉴。解决 方法:前期对主要功能进行快速原型设计、开发、验证和总结。本系列将记录从开发、部署、更新、卸载到ActiveX与JS间的交互的.Net开 发ActiveX全过程。由于之前学习如何使用.Net开发ActiveX时,查找了不少 文档,经过两天的东拼西凑后才掌握了整个开发过程,现在整理成系列以供日 后查阅。下面我们一起按部就班写ActiveX吧!二、写代码咯!开发环境:Win7、VS20101.创立类为 test.cab (或 test.exe ) , classic1为 clasid:xxxxxxxxxxxxxxxxxo 那么ActiveX控件的绝对路径就是(或 test.exe)/xxxxxxxxxxxxxxxo.NET Framework 2Q. .已安装的桎板/ Visual C# Windows Web Cloud Reporting Silverlight WCF Workflow wa其他顼目美型WHSWindows盍体应月程序ASP.NET Web 应用如Silverlight应用程序Silverlight 类H:名称(H):a(D:Visual C»Visual C9Visual C#Visual CaVisual CsVisual C»类型:Visual C«用于创立C*美取dl)的工程2.设置工程属性2.1. 在应用程序页中,翻开程序集信息,勾选使程序集COM可见2.2.在生成页中,勾选为COM互操作注册在 Properties.AssemblyInfo.cs 文件中 添加 Iassembly:AllowPartiallyTrustedCallers()' (注意引入:'System.Security'命名空间)3 .添加用户控件ActiveX以用户控件为载体,加载到网页中.添加控件的GUIDActiveX的用户控件均有一个独立的GUID标识,该GUID必须与工程的GUID不同。3.1. 通过VS2010-工具-创立GUID3.2. 弓|入'System.Runtime.InteropServices'4.3.将生成的 GUID 粘贴 到用户控件类声明前Guid(“4D39585B-7947-4197-8BDB-B0A6918B1098")public partial class ATC : UserControlInitializeComponent ();)4 .开发 lObjectSafety 接口为了让ActiveX控件获得客户端的信任,用户控件必须实现'lObjectSafety'接口 ,并且下面的代码是固定的(GUID也不能变)Comlmport, Guid("CB5BDC81-93C1TlCF-8F20-00805F2CD064")InterfaceType(ComInterfaceType. InterfacelsUnknown)public interface lObjectSafety(PreserveSigint GetlnterfaceSafetyOpt ions(ref Guid ri id, MarshalAs(CnmanagedType. U4) ref int pdwSupportedOptions, MarshalAs(UninanagedType. U4) ref int pdwhnab1edOp tions);PreserveSig ()int SetlnterfaceSafetyOptions(ref Guid riid, MarshalAs(CnmanagedType. U4) int dwOptionSetMask, MarshalAs(UnmanagedType. U4) int dwEnabledOptions);)5 .用户控件实现lObjectSafety接口GuidC4D39585B-7947-4197-8BDB-B0A6918B1098")public partial class ATC : UserControl, lObjectSafety(Ini t iali zeComponont();)region IObjectSafety 成员private const string _IID_IDispatch = " 00020400-0000-0000-C000-000000000046private conststring _IID_IDispatchEx = a6ef9860-c720T1dO-9337-00a0c90dcaa9”;private const stringIID_IPersistStorage = ?,0000010A-0000-0000-C000-000000000046)*;private const string _IID_IPersistStream =00000109-0000-0000-C000-000000000 046”;private const string IID IPersistPropertyBag = "37D84F60-42CBTICE-8135-00AA 004BB851”;private constint INTERFACESAFE_FOR_UNTRUSTED CALLER = 0x00000001;private constint INTERFACESAFE FOR_UNTRUSTED_DATA = 0x00000002;private const int S OK = 0;private constint E_FAIL = unchecked(int)0x80004005);private constint E_NOINTERFACE = unchecked(int)0x80004002);private bool _fSafeForScripting = true;private bool fSafeForlnitializing = true;public int GetlnterfaceSafetyOptions(ref Guid ri id, rof int pdwSupportedOption s, ref int pdwEneibledOptions)(int Rslt = EFAIL;string strGUID = ri id. ToString(,B,/);pdwSupportedOptions = INTERFACESAFE FOR INTRUSTED CALLER | INTERFACESAFE F0 R_UNTRUSTED_DATA;switch (strGUID)case _IID_IDispatch:case _IID_IDispatchEx:Rslt = S_OK;pdwEnab1edOpt i ons = 0;if (_fSafeForScripting = true)pdwEnablcdOptions = INTERFACESAFE_rOR_UNTRUSTED_CALLER;break;case _IID_IPersistStorage:case IIDIPcrsistStrcam:case IID IPersistPropertyBag:Rslt = SJ)K;pdwEnabIcdOpt ions = 0;if CfSafeForlnitializing = true)pdwEnab1edOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;break;default:Rslt = E_NOINTERFACE;break;)return Rslt;)public int SetlnterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int d wEnabledOptions)(int Rslt = E_FAIL;string strGUID = riid. ToString("B");switch (strGUID)case _IID_IDispatch:case IIDIDispatchEx:if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE FOR UNTR USTED_CALLER) &&(fSafeForScripting = true)Rslt = S_OK;break;case _IID_IPersistStorage:case _IID_IPersistStream:case IIDIPersistPropcrtyBag:if (dwEnabledOptions & dwOptionSetMask) = INTERFACESAFE FOR UNTR USTED_DATA) &&(fSafeForlnitializing = true)Rslt = S_OK;break;default:Rslt = E_NOINTERFACE;break;return Rslt;)ttendregion.获取 ActiveX 的 Classid5.1. 翻开VS2010->工具->OleView(假设没有就自行添加,程序路径:C:Program FilesMicrosoft SDKsWindowsv7.0AbinOleView.exe)o在'Object Classes'->'Grouped by Component Category'->'.NETCategory'找到刚才新建的ActiveX控件7.3.右键复制HTML标签6 .页面引用ActiveX控件在html页面上<object classid="clsid:ActiveX 控件的 clsid" codcbasc="控件打包后的 cxe 文件名或 cab 文件名"width="200px" height="200px”></object>6.1. classid :用于指定要加载的ActiveX的clsid , clasid就是用户控件的GUID值;8.2. codebase :用于指定clasid的基本URL ,可为绝对或相对路径,因ActiveX控件被打包到安装包中,所以codebase必须为安装包的路径。8.3. 实例:页面 URL 为 , codebase