因项目需要,需要开发一个功能:在IE中点击转跳,并打开chorme浏览器继续浏览指定页面。
分析需求后,参考了:
后,了解得知可从修改本地注册表配置和IE的actived控件两种方式实现,考虑到本地修改注册表无法使用js脚本打开链接,只能使用超链接转跳,与项目本身情况不符,故考虑自定义一个actived控件实现相关功能。
根据参考文献https://www.cnblogs.com/yilin/p/csharp-activex.html,我开始进行相关尝试:
1.创建接口IObjectSafety,以便让客户端信任自定义控件,其中Guid按照参考文件的要求固定为CB5BDC81-93C1-11CF-8F20-00805F2CD064,没有做修改。
[ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[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);
}
2.创建基类ActiveXControl
public abstract class ActiveXControl : IObjectSafety
{
#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
}
3.创建具体控件OpenChormeActiveX。指定的Guid值“a8d0d357-df7e-41a4-b963-1db5cfa67873”即为该ActiveX控件的唯一标识,保证其唯一性。
[Guid("a8d0d357-df7e-41a4-b963-1db5cfa67873")]
public class OpenChormeActiveX:ActiveXControl,IIEProperty
{
public void OpenChorme()
{
System.Diagnostics.Process.Start("Chrome.exe", "https://blog.csdn.net/sinat_23338865/article/details/65447387");
}
}
4.设置集的COM可见,设置类库项目属性->程序集信息->使程序集COM可见
5.在VS2017中下载扩展:Microsoft Visual Studio 2017 Installer Project,并创建一个单独的安装项目工程
6.右键点击新添加的安装项目,依次选择“添加->项目输出”菜单,打开添加项目输出组对话框,并选择ActiveX控件类库“CSharpActiveX”作为主输出
7.双击安装项目检测到的依赖项“Microsoft .NET Framework”,打开安装项目的启动条件界面,选中“.NET Framework”项,打开属性窗口,设置.NET Framework项的Version为“.NET Framework 4.0”
8.选中“主输出来自CSharpActiveX(活动)”项,设置主输出项内容的Register属性值为vsdrpCOM。
9.生成OpenChormeFormIE.msi
10.创建一个新文件夹makecab
11.将以下内容,放在cab.ddf中,放在makecab下
.OPTION EXPLICIT
.Set Cabinet=on
.Set Compress=on
.Set MaxDiskSize=CDROM
.Set ReservePerCabinetSize=6144
.Set DiskDirectoryTemplate="."
.Set CompressionType=MSZIP
.Set CompressionLevel=7
.Set CompressionMemory=21
.Set CabinetNameTemplate="OpenChormeFormIE.CAB"
"installer.inf"
"OpenChormeFormIE.msi"
12.将以下内容,放在installer.inf,放在makecab下
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec /i %EXTRACT_DIR%\OpenChormeFormIE.msi /qn
[Version]
Signature= "$CHICAGO$"
AdvancedInf=2.0
13.将OpenChormeFormIE.msi放在makecab下
14.在放在makecab下,启动cmd,执行makecab.exe /f "cab.ddf",生成OpenChormeFormIE.CAB
15.在html页面中,添加:
<object id="cSharpActiveX" classid="clsid:a8d0d357-df7e-41a4-b963-1db5cfa67873" codebase="dll/OpenChormeFormIE.CAB#version=1,0,0" style="display: none;"></object>
var o = document.getElementById("cSharpActiveX");
o.OpenChorme();