当前位置: 首页 > 科技观察

使用C#开发ActiveX控件,使用web调用

时间:2023-03-14 17:03:30 科技观察

已经使用快两个月了。我逐渐从学生变成了专业人士,我在慢慢积累知识,不断更新自己。在最近的一个项目中,涉及到一些问题,因为SDK只提供了winform使用,但是有需求就得自己完成,所以涉及到的ActiveX控件开发展示在web上,只是总结一些,之前我从来没有在学校接触过它。网上有教程,但大部分都有问题。只有自己通过了考验才能放心。一、开发ActiveX控件1、新建一个类库,将类库名称命名为“user.cs”;2、在类库中添加自定义用户控件“UserControl1”,实现各种自定义功能;3.为了解决浏览器安全设置对控件的影响,必须在组件中添加IObjectSafety接口,所以再添加一个接口类“IObjectSafety.cs”usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem。Web.UI.WebControls.WebParts;//必须引用包usingSystem.Security;usingSystem.Runtime.InteropServices;//必须引用包命名空间user{[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]//GUID不需要修改,固定的publicinterfaceIObjectSafety{//方法定义voidGetInterfacceSafyOptions(System.Int32riid,outSystem.Int32pdwSupportedOptions,outSystem.Int32pdwEnabledOptions);voidSetInterfaceSafetyOptions(System.Int32riid,System.Int32dwOptionsSetMask,System.Int32dwEnabledOptions);}}4、继承接口publicpartialclassUserControl1:UserControl,IObjectSafety{publicUserControl1(){InitializeComponent();}publicvoidGetInterfaceSafyOptions(System.Int32riid,outSystem.Int32pdwSupportedOptions,outSystem.Int32pdwEnabledOptions){pdwSupportedOptions=1;//不修改代码pdwEnabledOptions=2;//不修改代码return;}publicvoidSetInterfaceSafetyOptions(System.Int32riid,System.Int32dwOptionsSetMask,System.Int32dwEnabledOptions){return;}Ourpublic(vo)5.在UserControl1中引入两个命名空间  usingSystem.Security;  使用System.Runtime.InteropServices;6.Tools——CreateGUID——NewGUID——选择第五项——Copy,可以关闭小窗口,然后粘贴到namespace下,如下namespaceuser{[Guid("7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7")]//这个GUID就是publicpartialclassUserControl1:UserControl,IObjectSafety{……7.最后一步,project——用户属性(最后一项),有两个地方需要修改①Application——Assembly信息——√使程序集COM可见②生成——√COM互操作注册8.可以在项目上右键user-generate2.web使用ActiveX控件调用web很简单,引用刚才生成的dll文件,然后添加

即可完成注意:这里classid中的字符串中有"clsid:",别忘了,跟上了通过第六步生成的GUID,一定要一致!