C# 编写的ActiveX控件

  以前从未接触过ActiveX,今天做个整理。

首先ActiveX是用来扩充丰富在web上的应用,即使自己在winform下编写的用户控件,它也能够在网页上发挥作用(好吧,ActiveX毁了我的世界观)。

  接着,我们创建一个windows窗体控件库,代码设计如下

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 ActiveXSample1
{
    [Guid("0e8c5166-c7f6-48dc-8519-a9fb8f964a7f")]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string ShowMsg()
        {
            return "HelloWorld:I am Heaven";
        }
    }
}

至于为什么使用GUID,我个人认为那是为了在网页中能准确地找到程序集。编译之后,我们会得到一个ActiveXSample1.dll,将之拷贝到一个asp.net项目下,在该项目下创建一个网页webform.aspx,网页代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ForActiveX.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button" onclick="alert(helloworld.ShowMsg());" />
        <object id="helloworld" classid="clsid:0e8c5166-c7f6-48dc-8519-a9fb8f964a7f"/>
    </div>
    </form>
</body>
</html>

注意object标记,其中classid就是刚才我们使用的GUID。程序写到这里可以运行一下,能看到用户控件的外观。但是,当想要用html元素调用ActiveX内的方法是,这儿网页要弹出一些提示,这儿必须上图,应为这里我卡了很久,相信有很多同学也卡在这里

要能够使用button,那么在IE,Internet选项中,安全选项卡,在本地Intranet中如图设置,

这还不够,可信站点中添加本站点,,如此便可运行!

如果不能用input调用ActiveX的方法,那么主要还是在internet选项的设置上,如果没能了解清楚的同学多在这上面琢磨琢磨!这是一种比较拙劣的方法,测试一下ActiveX控件可以,但是实际运用中不这样,因为这样很不安全,较为安全的做法,那就是给ActiveX签名加证书。如何给ActiveX加证书请看最后!

还有注意:win764位机上你可能装了IE64位,64位目前是不能够现实ActiveX的,现在我的电脑上有64位和32位版本的IE,64位的无法显示,详细请看http://www.iefans.net/activex-kongjian-wufa-jiazai/

另外,为了安全起见,必须实现IObjectSafety接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ActiveXSample1
{
    [Guid("c44c9c1a-e905-43d0-8792-b1c1e80b869d")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IObjectSafety
    {
        [PreserveSig]
        void GetInterfacceSafyOptions(
        int riid,
        out int pdwSupportedOptions,
        out int pdwEnabledOptions);

        [PreserveSig]
        void SetInterfaceSafetyOptions(
        int riid,
        int dwOptionsSetMask,
        int dwEnabledOptions);
    }
}

首先说打包成cab文件,我们需要一套工具,工具在这里https://files.cnblogs.com/HelloMyWorld/Cab%E5%88%B6%E4%BD%9C%E5%B7%A5%E5%85%B7.rar

具体教程在这里http://blog.csdn.net/logenliqiang/article/details/5897204

根据上面的教程可以制作cab文件

现在我们来签名,运行工具包里的signcode.exe,选中我们的cab文件

然后签名完成。

在放到web服务器上运行,不用修改安全选项,它会提示你ActiveX已被阻止安装,那是因为没有安装证书(小可是在虚拟机里测试的,要在虚拟机里加入证书),将证书导入到2个区域

一个是

另一个是

导入后再重新加载页面,发现不用修改安全选项也能显示ActiveX控件了

 参考网页

http://yusy1116.blog.163.com/blog/static/6467259220103120151695/

http://www.cnblogs.com/prowyh/archive/2012/12/04/2802275.html

原文地址:https://www.cnblogs.com/HelloMyWorld/p/2860524.html