开发环境:vs2008
第一步 创建项目
新建一个项目,选择“windows窗体控件库”,创建一个用户控件项目“activexdemo”(注意,这里起名不能用中文,否则后面会出问题),里面有个用户控件类usercontrol1.cs
在类中写上你自己需要的业务逻辑代码,保存
第二步 设置项目属性
在assemblyinfo.cs里添加[assembly: allowpartiallytrustedcallers()],需要引用using system.security;命名空间
设置项目属性,右键项目——属性
选择“应用程序”,点开“程序集信息”
勾选“使程序集com可见”,点“确定”
然后选择“生成”,滚动拉倒底部,勾选“为com互操作注册”
然后保存,重新编译项目,至此,此时的“activexdemo.dll”就成了一个activex控件
第三步 安装外部工具
安装外部工具“ole/com对象查看器”和“创建guid”(已有这两款工具的可以忽略此步骤)

1.ole/com对象查看器
这款工具是用来查看activex控件的,也就是验证你的项目有没有成为一个activex控件
安装方法:点开vs顶部菜单栏“工具”——“外部工具”
然后,添加一个“ole/com对象查看器”,对应的命令程序一般放在c:program filesmicrosoft sdkswindowsv6.0abinoleview.exe
点击“应用”,“确定”,这就算是安装完成,再重新点开顶部的“工具”菜单看看,里面就有一项“ole/com对象查看器”
点击“ole/com对象查看器”,展开左侧的“.net category”
你会发现,这里面就有你刚刚创建的activex控件
这就是ole/com对象查看器的作用
2.创建guid
高版本的vs貌似都自带了这个工具,但是在有的低版本或者安装不完整比如vs2008中不一定有这个工具,所以在没有的时候需要安装一下
安装方法同上,只不过命令程序的地址一般是c:program files (x86)microsoft visual studio 9.0common7toolsguidgen.exe
此工具的用途在下面的步骤中会讲到
第四步 提高activex插件的安全等级
ie怎么知道一个插件是脚本安全的?它是通过以下两个办法。
一是查询activex组件是否实现了iobjectsafety接口,并且返回脚本安全;
二是查询activex组件是否在注册表的component category manager里表明自己实现了catid_safeforinitializing和catid_safeforscripting。
这里我们只说第一种实现iobjectsafety接口
首先,为控件类usercontrol1添加一个guid,这个编号将用于b/s系统的客户端调用时使用(可以使用 工具-创建guid 菜单创建一个guid):
using system;
using system.collections.generic;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;
namespace activexdemo
{
[guid("bb725724-65d6-4e71-aa11-dedfafe9248f"), progid("activexdemo.usercontrol1"), comvisible(true)]
public partial class usercontrol1 : usercontrol,iobjectsafety
{ 注意,要引入system.runtime.interopservices;命名空间
其次,为了让activex控件获得客户端的信任,控件类还需要实现一个名为“iobjectsafety”的接口,因此在项目中添加一个接口类iobjectsafety
直接将下列代码复制粘贴,不要作任何改动,尤其是guid,都是固定的,不能改
using system; using system.collections.generic; using system.linq; using system.text; using system.runtime.interopservices; namespace activexdemo { [comimport, guidattribute( " cb5bdc81-93c1-11cf-8f20-00805f2cd064 " )] [interfacetypeattribute(cominterfacetype.interfaceisiunknown)] public interface iobjectsafety { [preservesig] int getinterfacesafetyoptions(ref guid riid, [marshalas(unmanagedtype.u4)] refint pdwsupportedoptions, [marshalas(unmanagedtype.u4)] refint pdwenabledoptions); [preservesig()] int setinterfacesafetyoptions(ref guid riid, [marshalas(unmanagedtype.u4)] int dwoptionsetmask, [marshalas(unmanagedtype.u4)] int dwenabledoptions); } }
同样,需要引入system.runtime.interopservices;命名空间
接着,在控件类usercontrol1中实现iobjectsafety的接口
#region iobjectsafety 成员 privateconststring _iid_idispatch = "{00020400-0000-0000-c000-000000000046}"; privateconststring _iid_idispatchex = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; privateconststring _iid_ipersiststorage = "{0000010a-0000-0000-c000-000000000046}"; privateconststring _iid_ipersiststream = "{00000109-0000-0000-c000-000000000046}"; privateconststring _iid_ipersistpropertybag = "{37d84f60-42cb-11ce-8135-00aa004bb851}"; privateconstint interfacesafe_for_untrusted_caller = 0x00000001; privateconstint interfacesafe_for_untrusted_data = 0x00000002; privateconstint s_ok = 0; privateconstint e_fail = unchecked((int)0x80004005); privateconstint e_nointerface = unchecked((int)0x80004002); privatebool _fsafeforscripting = true; privatebool _fsafeforinitializing = true; publicint getinterfacesafetyoptions(ref guid riid, refint pdwsupportedoptions, refint 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; } publicint 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
这些代码也是固定的,不能改动,直接复制粘贴就行了
第五步 制作成安装文件
在原项目解决方案中新建一个安装项目,在vs2008中,是这样操作的
新建项目——其他项目类型——安装和部署——安装项目
注意,这里下方“解决方案”这里要选择“添入解决方案”
然后,右键安装项目——添加——项目输出
将我们的项目activexdemo设为主输出
点确定,然后在主输出文件上右键——属性
将register属性设为vsdrpcom
重新编译安装项目,打开安装项目所在目录
至此,activex浏览器插件的安装包就制作好了,双击setup.exe文件或activexdemosetup.msi文件可以将浏览器插件安装到你的电脑
第六步 使用activex插件
新建一个web项目或者一个html文件,在需要使用浏览器插件的页面上加入以下代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<object id="csharpactivex" classid="clsid:bb725724-65d6-4e71-aa11-dedfafe9248f" width="100%" height="150"></object>
<form id="form1" runat="server">
<div>
<input type=‘button‘ onclick=‘csharpactivex.test()‘ value=‘我是按钮‘/>
</div>
</form>
</body> 其中,重点是object标签里的classid属性,属性里面的guid对应的就是第四步中指定的guid
直接使用object定义的id属性就可以调用usercontrol1类中的方法,是不是很方便?
为了更方便直观的看出插件有没有正常加载,可以再加入一些检测代码,如下:
<body>
<object id="csharpactivex" classid="clsid:bb725724-65d6-4e71-aa11-dedfafe9248f" width="100%" height="150"></object>
<form id="form1" runat="server">
<div>
<input type=‘button‘ onclick=‘csharpactivex.test()‘ value=‘我是按钮‘/>
</div>
</form>
</body>
<script type="text/javascript">
var objcard = document.getelementbyid("csharpactivex");
if (objcard.object==null) {
alert("csharpactivex插件未安装!");
}
else{
alert("已检测到csharpactivex插件!");
}
</script>
</html> 然后,由于只有ie支持activex浏览器插件,所以在ie浏览器中打开这个页面,看一下效果
这时,不放心的话,可以再检查咱们的activex插件究竟有没有安装上
点开工具栏“工具”——“管理加载项”
可以看到其中有一项activexdemo.usercontrol1,这就是我们安装上去的浏览器插件
至此,恭喜你已成功掌握了制作activex浏览器插件的技能~
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!