文章属于原创,转载请联系本人。有参照两个博客(http://blog.csdn.net/seanbei/article/details/53692899)
1、ActiveX插件的制作
环境:vs2010,cert2spc.exe,makecert.exe,signtool.exe,signcode.exe makecab.exe
1.1、创建
创建选择C# -> Windows Forms Control Library,点击finish
1.2、使程序集COM可见
右键项目——属性——应用程序——程序集信息,然后选中“使程序集COM可见”
点击生成,然后选中”为COM互操作注册“,如下图所示:
1.3、添加用户控件
ActiveX项目右键->添加->用户控件,取名demo
为button1添加Click事件
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.textBox1.Text);
}
1.4、创建GUID
vs2010菜单栏->工具->创建GUID(选5)
我的项目的GUID为[Guid("A886B6E3-9FE0-45F3-B11E-E94C68DDDA92")]
1.5、添加IObjectSafety接口
右键项目——添加类——接口—输入IObjectSafety,直接点击Add。
在接口中输入以下代码:
注意:这个Guid不用修改,直接复制
[ComImport, Guid("1D9AD540-F2C9-4368-8697-C4AAFCCE9C55")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
1.6、继承IObjectSafety接口
修改demo控件类继承IObjectSafety接口
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;//Guid引用
using Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//using OpenQA.Selenium.IE.InternetExplorerDriver;
using OpenQA.Selenium.PhantomJS;
using System.Diagnostics;
namespace ActiveXForm
{
[Guid("A886B6E3-9FE0-45F3-B11E-E94C68DDDA92")]
public partial class demo : UserControl, IObjectSafety
{
public demo()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IWebDriver selenium = new InternetExplorerDriver();
selenium.Navigate().GoToUrl("https://hdrk-test.fs.em2.oraclecloud.com/homePage/faces/FuseWelcome");
Process[] p = Process.GetProcessesByName("IEDriverServer"); //根据进程名获得指定的进程
if (selenium.Title == "Sign In")
{
selenium.FindElement(By.Id("userid")).SendKeys("techadmin@hytera.com");
selenium.FindElement(By.Id("password")).SendKeys("Hytera2017");
selenium.FindElement(By.Id("btnActive")).Click();
}
Console.WriteLine(selenium.PageSource);
p[0].Kill(); //杀死该进程
}
private string GetPhantomJSDriverService()
{
throw new NotImplementedException();
}
public string str()
{
return this.textBox1.Text;
}
#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, ref int pdwSupportedOptions, 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, 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
private void demo_Load(object sender, EventArgs e)
{
}
}
}
项目运行,至此,ActiveX插件就算完成
1.7、新建一个安装项目
Other Project Types -> Visual Studio Installer -> Setup Project
1.8、项目输出
在项目上点右键,【添加】->【项目输出】,选择上边的项目
双击箭头所指
选择主输出右键属性窗口,设置Register为vsdrpCOM
项目运行,Debug目录下有两个文件,exe文件和msi文件
1、打包成CAB文件
2.1、编写inf文件
[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\Setup2.msi" /qn
msi修改成自己的即可
2.2、编写bat文件
新建txt文件,加入以下内容,将文件名重新命名build.bat即可。其中Activex.cab是生成目标cab的名称;install.inf是第一步生成的文件名,而Setup2.msi是需要打包的安装程序名;第二条ping命令仅仅是让批处理不要那么快退出,起到更利于观察生成结果的作用。
"cabarc.exe" -s 6144 n ActiveX.cab ActiveX.inf Setup2.msi
ping -n 20 127.0.0.1 >nul
确保inf、msi文件,cabarc.exe在同一个文件下,点击bat文件,即可生成CAB文件
2.3、创建证书和私钥
使用命令makecert.exe创建证书和私钥文件(先放入cert2spc.exe,makecert.exe,signtool.exe)
在当前文件夹下执行下面命令(n-证书颁发对象)
makecert -sv "ActiveXHello.pvk" -n "CN=lhsoft.net" -r ActiveXHello.cer
输入两次一样的密码;
1、为CAB进行签名
用signcode.exe进行签名
3.1、选择cab文件
3.2、选择自定义
3.3、选择cer证书
选择刚刚生成的cer证书
3.4、选择秘钥
默认选择磁盘上的私钥文件
3.6、填入数据描述
Windows,http://www.microsoft.com
3.7、时间戳服务器URL
填入免费的时间戳服务器URL http://timestamp.wosign.com/timestamp
4. 最后
自己创建htm文件,内容:
<html>
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function aa() {
alert(document.getElementById('Object1').str());
}
</script>
</head>
<body>
<h1>Portal</h1>
<object id="helloworld" classid='clsid:A886B6E3-9FE0-45F3-B11E-E94C68DDDA92' width="184" height="150" CODEBASE=ActiveX.cab##FileVersion=1,0,0,1></object>
</body>
</html>
打开htm页面,下载即可。
想用C++实现可以跳转到 http://www.cnblogs.com/huangd/p/8021865.html