文章屬于原創,轉載請聯系本人。有參照兩個部落格(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
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcucjNzIjMwIDO30yNzATOwAzM2EDOxITM3EDMy0CM4UjM4ITMvwlMxcTMwIzLcBDO1IDOyEzLcd2bsJ2Lc12bj5ycn9Gbi52YucTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
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("[email protected]");
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
轉載于:https://www.cnblogs.com/huangd/p/8058338.html