之前只是接触过ActiveX控件的使用,并不知ActiveX控件是怎么生成的。现在因为项目需要,记录一下ActiveX的制作过程。(部分参考 另一篇博客http://www.cnblogs.com/yungboy/archive/2011/01/10/1932433.html)
1.创建Windows窗体控件库,项目取名为ActiveX(创建解决方案不再赘述)
2.在ActiveX项目上右键->属性->应用程序选项->程序集信息,勾选最下面的“使程序集COM可见”,点击确定
3.仍然是ActiveX项目的属性->生成,勾选最下面的“为COM互操作注册”
(注意Debug和Release模式,使用哪种模式就将哪种模式下的“为COM互操作注册”勾选)
4.打开ActiveX项目Properties->AssemblyInfo.cs文件,添加using System.Security;引用,添加[assembly: AllowPartiallyTrustedCallers()]代码
5.为项目添加用户控件,ActiveX项目右键->添加->用户控件,取名demo
设计demo界面如下
为button1添加Click事件
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.textBox1.Text);
}
用户控件设计基本完成,ActiveX的引用需要用到Guid,下面我们来生成Guid
1.vs2010菜单栏->工具->创建GUID
(我的项目的GUID为[Guid("3509DA26-0480-4CB5-B791-15FB6C1B7FAC")],下面以此为例,上图仅供参考)
2.为了让ActiveX控件获得客户端的信任,控件类还需要实现一个名为“IObjectSafety”的接口
直接复制下面代码即可,注意其中的Guid不需要修改,且需要添加using System.Runtime.InteropServices;引用
[ComImport, Guid("1D9AD540-F2C9-4368-8697-C4AAFCCE9C55")] [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); }
3.修改demo控件类继承IObjectSafety接口,代码如下
[Guid("3509DA26-0480-4CB5-B791-15FB6C1B7FAC")] public partial class demo : UserControl,IObjectSafety { public demo() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this.textBox1.Text); } public string str() { return this.textBox1.Text; } #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 }
至此,ActiveX控件基本完成,下面打包ActiveX
1.解决方案创建安装工程
2.查看安装项目的文件系统,点击应用程序文件,在右侧右键添加项目输出,选择刚才创建的ActiveX项目,选择主输出
3.创建项目输出之后,应用程序文件的右侧栏中,选择主输出右键属性窗口,设置Register为vsdrpCOM
4.设置完成之后,生成安装工程Setup1,应用程序文件右侧将有两个文件,如下
这样打包文件生成了,Debug目录下有两个文件,exe文件和msi文件,如果想生成cab文件,则可以网上查询下如何生成cab文件(需要借助别的工具)
至此ActiveX的打包工作已经做完,下面我们来使用这个ActiveX控件
1.创建一个Web项目WebApplication1,然后再Default.aspx页面添加对ActiveX控件的引用,其中clsid为上面生成的GUID
<object id="helloworld" classid='clsid:3509DA26-0480-4CB5-B791-15FB6C1B7FAC' width="184" height="150" ></object>
页面如下
另外,除了单纯的使用ActiveX控件,可能还需要利用Js手动调用ActiveX的方法,上面demo.cs的代码中,我定义了一个公开的str方法,web页面利用Js调用str方法,如下
<script type="text/javascript">
function aa() {
alert(document.getElementById('Object1').str());
}
</script>
<object id="helloworld" classid='clsid:3509DA26-0480-4CB5-B791-15FB6C1B7FAC' width="184" height="150" ></object>
<object id="Object1" classid='clsid:3509DA26-0480-4CB5-B791-15FB6C1B7FAC' width="0" height="0" ></object>
<input type="button" value="点击" onclick="aa()"/>
请注意两个ActiveX的id和宽高是不一样的,页面如下
OK,C#制作ActiveX控件以及web引用和Js简单调用记录完成,希望可以帮助有需要的小伙伴,同时也方便自己日后查看。(*^__^*)
这个亲测有效,本人系统环境 win10 64位,开发工具vs2010,.NET Framework 4.0
初步了解,涉及较浅,部分参考 另一篇博客http://www.cnblogs.com/yungboy/archive/2011/01/10/1932433.html
如转载请注明出处http://www.cnblogs.com/wyynts/p/6874387.html,谢谢