C#制作ActiveX插件

时间:2021-10-11 04:05:52
  1. 首先新建项目--->类库,取名:ActiveXDemo
  2. C#制作ActiveX插件
  3. 右键项目属性:应用属性==>程序集信息=>使程序集Com可见,
  4. C#制作ActiveX插件
  5. 生成==>输出==>为com互操作注册
  6. 新建接口类取名:IObjectSafety,以下代码可直接用,最好不要修改
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text; namespace ActiveXDemo
{
[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);
}
}

新建用户控件取名:UserControl1,UserControl1继承自上面新建的接口IObjectSafety,并实现接口(实现接口的方法请复制下面的内容),在类UserControl1添加Guid特性值,利用VS的工具生成guid

C#制作ActiveX插件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace ActiveXDemo
{
[Guid("C5BD015D-E2AA-4DD3-AEB7-93D7409FA751")]
public partial class UserControl1 : UserControl, IObjectSafety
{
public UserControl1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("ActiveXDemo");
} public string GetGUID()
{
return Guid.NewGuid().ToString();
} #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 = ;
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, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] 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 = ;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = ;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Rslt = E_NOINTERFACE;
break;
} return Rslt;
} public int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] 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
}
}

进行简单界面布局:

C#制作ActiveX插件

点击按钮效果见上面代码,还可自己写一个公共方法,我写的是GetGUID(),

C#制作ActiveX插件

生成成功将会生成两个文件:ActiveXDemo.dll和ActiveXDemo.tlb,至此ActiveX浏览器插件制作完成,

以下是如何打包安装ActiveX插件,

我用的InstallShield2010破解版制作安装包

新建InstallScript Msi类型工程

最主要的是在添加文件时候,要把ActiveXDemo.tlb这个文件设为自注册

C#制作ActiveX插件

在生成安装文件时候,可以选择生成适合网络形式的或者单个安装包,在这里我生成了单个安装包,你也可以选择生成网络的

C#制作ActiveX插件

点击生成安装包按钮,生成后如下:

C#制作ActiveX插件

至此,安装包制作完成,我们点击安装后他会自动注册tlb文件.

以下是如何使用ActiveX插件:

新建一个html页面:我取名:TestActiveX.html

内容如下:

 <!DOCTYPE html>

 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<Script type="text/javascript">
function GetGUID() {
var a= document.getElementById("ActiveXDemo").GetGUID();
alert(a);
} </Script>
</head>
<body>
<object id="ActiveXDemo" classid='clsid:C5BD015D-E2AA-4DD3-AEB7-93D7409FA751' width="350" height="350"></object> <input id="Button1" type="button" value="获取guid" onclick="GetGUID()" />
</body>
</html>

注意这里的classid要和你的插件上生成的guid一致才可以
查看页面效果:

C#制作ActiveX插件

C#制作ActiveX插件

到此,结束,关于用InstallShield制作安装包的详细过程,请自行学习.