ActiveX 技术总结
创建接口 IObjectSafety
注意
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
这个 guid是死的,所有地方都一样
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IObjectSafety { // methods void GetInterfacceSafyOptions( System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions); void SetInterfaceSafetyOptions( System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions); } public abstract class ActiveXControl : UserControl,IObjectSafety { const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 1; const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 2; public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions) { pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; } public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions) { } }
创建用户控件类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[Guid("bcd0a305-5e3c-44d8-b6ff-c2ab256576ad")] public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("这是一个com组件ActiveXControl"); } }
这个 guid 可用Guid.NewGuid().ToString();获取,这个值最终html中 为控件唯一标志
接下来制作安装包
添加项目主输出
注意
这样做出来的安装包,在程序安装之后就自动在注册表注册了
最后用网页测试
<OBJECT ID="docScan1" WIDTH="100%" HEIGHT="100%" CLASSID="CLSID:bcd0a305-5e3c-44d8-b6ff-c2ab256576ad">
</OBJECT>