c#中开发ActiveX的学习笔记
Posted 野狼谷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中开发ActiveX的学习笔记相关的知识,希望对你有一定的参考价值。
1.为什么要用ActiveX?
网页本身的功能是有限的,要想实现一些网页本身不支持的功能,比如:网页上的p2p视频播放,就得靠ActiveX这种古老的技术。
2.c#能开发ActiveX吗?
严格意义上讲,c#是不能生成纯正ocx控件的,我们在vs.net中新建项目时,也找不到专门的"ActiveX项目"新建项,最多也只就能新建"类库"得到一个dll而非ocx(因此我们也无法用传统的regsvr32来注册该dll),但是c#能开发com组件,activeX控件本质上讲跟com是一类技术,所以用c#开发"能够让网页调用的com类库"还是可行的。
3.开发步骤:
(1)新建一个类库 (2)修改项目的"属性",在“生成”选项中把“输出”中的“为com互操作注册”勾中,然后再到“应用程序”选项中找到“程序集信息”按钮,点击它,在弹出的界面中勾中“使程序集COM可见(M)”
(3)修改AssemblyInfo.cs,增加[assembly: AllowPartiallyTrustedCallers()],完整内容类似下面这样:
1 using System.Reflection; 2 using System.Runtime.CompilerServices; 3 using System.Runtime.InteropServices; 4 using System.Security; 5 6 // General Information about an assembly is controlled through the following 7 // set of attributes. Change these attribute values to modify the information 8 // associated with an assembly. 9 [assembly: AssemblyTitle("ActiveXDemo")] 10 [assembly: AssemblyDescription("")] 11 [assembly: AssemblyConfiguration("")] 12 [assembly: AssemblyCompany("Microsoft")] 13 [assembly: AssemblyProduct("ActiveXDemo")] 14 [assembly: AssemblyCopyright("Copyright ? Microsoft 2009")] 15 [assembly: AssemblyTrademark("")] 16 [assembly: AssemblyCulture("")] 17 [assembly: AllowPartiallyTrustedCallers()] 18 19 // Setting ComVisible to false makes the types in this assembly not visible 20 // to COM components. If you need to access a type in this assembly from 21 // COM, set the ComVisible attribute to true on that type. 22 [assembly: ComVisible(true)] 23 24 // The following GUID is for the ID of the typelib if this project is exposed to COM 25 [assembly: Guid("bd585d12-7f22-4b3f-959f-18efbfc53f94")] 26 27 // Version information for an assembly consists of the following four values: 28 // 29 // Major Version 30 // Minor Version 31 // Build Number 32 // Revision 33 // 34 // You can specify all the values or you can default the Build and Revision Numbers 35 // by using the \'*\' as shown below: 36 // [assembly: AssemblyVersion("1.0.*")] 37 [assembly: AssemblyVersion("1.0.0.0")] 38 [assembly: AssemblyFileVersion("1.0.0.0")]
(4)新建一个IObjectSafety接口文件IObjectSafety.cs,内容如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Runtime.InteropServices; 5 6 namespace ActiveXDemo 7 { 8 [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")] 9 [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] 10 public interface IObjectSafety 11 { 12 [PreserveSig] 13 int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions); 14 15 [PreserveSig()] 16 int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions); 17 } 18 19 }
该内容除命名空间可以更改外,其它内容都是固定的,不要修改 (5)新建一个:Windows Forms-->“用户控件”,我们的主要逻辑就写在这里(还可以在它上面随便放置其它windows常用控件,跟winForm开发一样),不过首先要修改类定义,让其实现我们刚才定义的接口
1 using System; 2 using System.Runtime.InteropServices; 3 using System.Threading; 4 using System.Windows.Forms; 5 6 7 namespace ActiveXDemo 8 { 9 [Guid("8d7d8518-ca58-4863-b94d-3c616fda7b35")] 10 public partial class MyActiveX : UserControl,IObjectSafety 11 { 12 delegate void D(object obj); 13 14 public MyActiveX() 15 { 16 InitializeComponent(); 17 } 18 19 #region IObjectSafety 成员 20 21 private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"; 22 private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; 23 private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}"; 24 private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}"; 25 private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; 26 27 private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; 28 private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; 29 private const int S_OK = 0; 30 private const int E_FAIL = unchecked((int)0x80004005); 31 private const int E_NOINTERFACE = unchecked((int)0x80004002); 32 33 private bool _fSafeForScripting = true; 34 private bool _fSafeForInitializing = true; 35 36 public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions,