C#创建activex供js调用
建立vsstudio 工程
点击 新建 -》项目-》window控件库 【名称自己随便取 比如:UserControl1.cs】
首先 建立window 安全接口【为了防止ie浏览器阻止activex】
同时在 -》properties 中的文件AssemblyInfo.cs 文件中添加一行
[assembly: AllowPartiallyTrustedCallers()] //引入using System.Runtime.InteropServices;
接口名称 IObjectSafety
[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); }
UserControl1.cs 继承该接口
在类中添加如下代码
#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
点击visio studio 工具-》创建uuid -》点击copy
UserControl1.cs 添加注解[Guid("10636708-BF45-4f9b-8882-CAB52B25C48B")]
10636708-BF45-4f9b-8882-CAB52B25C48B 是刚刚copy的内容 copy时有{} 去掉它
比如:
[Guid("10636708-BF45-4f9b-8882-CAB52B25C48B")]
public partial class UserControl1 : UserControl,IObjectSafety
点击工程 点击属性 应用程序-》输出类型改为类库,记录下 程序集空间 ,-》点击程序集信息 勾上 使程序集com可见
生成-》勾上 ‘为com Interop 注册’
现在在UserControl1.cs 加入如下方法
public string showSaveFile(string defaultStr)
{
SaveFileDialog d = new SaveFileDialog();
d.FileName = defaultStr;
if (d.ShowDialog() == DialogResult.OK)
{
this.lblname.Text = d.FileName;
}
return d.FileName;
}
再在UserControl1.cs 加几个按钮 文本框啥的
编译 生成了一个dll 文件 将它丢入system32 目录下
在任意目录 建立一个 test.html
让控件在页面上显示 可以在在body中添加
<object classid="clsid:上面生成的uuid" width="300" height="300"/>
在js中调用
<html>
<script>
function init()
{
var c=new ActiveXObject("FirstActivex.UserControl1");
var path=c.showSaveFile("c.txt");
}
</script>
<body onload="init()">
</body>
</html>
FirstActivex是程序集空间 UserControl1是activex控件名称showSaveFile("c.txt");
是刚刚在c#控件中定义的方法