标签:
http://www.cnblogs.com/still-windows7/p/3148623.html
最近有这样一个需求,需要在网页上面启动客户端的软件,软件之间的通信、调用,单单依靠HTML是无法实现了,因此必须借用Activex来实现。由于本 人主要擅长C#,自然本文给出了用C#实现的范例,本文的预期效果是有一定Winform基础的人可都轻松读懂本文。


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace EasyActivex
{
[ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceTypeAttribute(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);
}
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace EasyActivex
{
//这个Guid,网页调用的时候用到,Mark
[Guid("685F0A47-944D-4145-BF4E-76A02A422B02")]
//这里要实现IObjectSafety接口
public partial class EUserControl : UserControl, IObjectSafety
{
public EUserControl()
{
InitializeComponent();
}
#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
/// <summary>
/// 打开记事本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenNote_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe");
}
}
}
小提示:EUserControl代码的Guid可以用VS附带的Guid生成工具生成:


//用户添加 [assembly: AllowPartiallyTrustedCallers()]




[version] signature="$CHICAGO$" AdvancedINF=2.0 [Setup Hooks] hook1=hook1 [hook1] run=msiexec.exe /i "%EXTRACT_DIR%\EasySetup.msi" /qn
2) build.bat制作。新建txt文件,加入以下内容,将文件名重新命名build.bat即可。其中EasyActivex.cab是生成目标cab 的名称;install.inf是第一步生成的文件名,而EasySetup.msi是需要打包的安装程序名;第二条ping命令仅仅是让批处理不要那么 快退出,起到更利于观察生成结果的作用。
"cabarc.exe" -s 6144 n EasyActivex.cab install.inf EasySetup.msi ping -n 20 127.0.0.1 >nul



<object id="csharpActiveX" codebase="Activex/EasyActivex.cab" classid="clsid:685F0A47-944D-4145-BF4E-76A02A422B02"></object>
运行效果如下:

点击即可在网页中打开记事本了。
六、参考资料:
1、使用C#开发ActiveX控件 http://www.cnblogs.com/yilin/archive/2009/09/15/1567332.html
2、Activex签名方法和工具技巧 http://www.360doc.com/content/10/0901/15/203871_50402416.shtml
3、中国数字认证网用户手册 http://www.ca365.com/forward.do?pageurl=/ca/yhsc.jsp
标签:
原文地址:http://www.cnblogs.com/mazhenyu/p/5481409.html