• ActiveX插件


    C#制作ActiveX插件

    首先新建项目--->类库,取名:ActiveXDemo

    右键项目属性:应用属性==>程序集信息=>使程序集Com可见,

    生成>输出>为com互操作注册
    新建接口类取名:IObjectSafety,以下代码可直接用,最好不要修改
    复制代码
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Runtime.InteropServices;
    5 using System.Text;
    6
    7 namespace ActiveXDemo
    8 {
    9 [ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
    10 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    11 public interface IObjectSafety
    12 {
    13 [PreserveSig]
    14 int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
    15 [PreserveSig()]
    16 int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
    17 }
    18 }
    复制代码
    新建用户控件取名:UserControl1,UserControl1继承自上面新建的接口IObjectSafety,并实现接口(实现接口的方法请复制下面的内容),在类UserControl1添加Guid特性值,利用VS的工具生成guid

    复制代码
    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 = 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, [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 = 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, [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
    }
    

    }
    复制代码

    进行简单界面布局:

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

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

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

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

    新建InstallScript Msi类型工程

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

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

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

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

    以下是如何使用ActiveX插件:

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

    内容如下:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    14
    15
    16
    17
    18
    19
    20
    复制代码
    注意这里的classid要和你的插件上生成的guid一致才可以
    查看页面效果:

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

  • 相关阅读:
    从零开始学SQLSERVER-游标CURSOR(基础)
    从零开始学SQLSERVER-创建表
    从零开始学HTML-button
    从零开始学前端HTML-input标签
    专项测试实战 | 如何测试 App 流畅度(基于 FPS 和丢帧率)
    接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
    软件测试之30分钟轻松搞定正则表达式基础
    Python 装饰器填坑指南 | 最常见的报错信息、原因和解决方案
    react16与react15之间的区别
    react-父子组件之间的交互传值
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/8806396.html
Copyright © 2020-2023  润新知