赞
踩
1.打开VS2010,创建一个Windows控件库项目
2.创建一个IObjectSafety接口,防止IE弹出不安全提示
[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);
}
3.在项目中添加用户控件,并实现上面接口,并为控件分配一个GUID,在HTML是要用到的
,另外还要引用命名空间:using System.Runtime.InteropServices;
- [Guid("0D69FA27-592D-4B7E-BF7D-A0BCDC54D1BC")]//直接创建一个就可以,或用工具,点创建GUID,再复制过来
- public partial class TestUc : UserControl,IObjectSafety
- {
- public string UID { get; set; }
-
- public TestUc()
- {
- InitializeComponent();
- }
-
- public string Test(string abc)
- {
- UID = abc;
- return "TestUc:This is a " + abc;
- }
- #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
- }
4.创建安装程序
在该解决方案上继续添加新工程,选择其他项目类型,选择安装工程
然后设置下属性Register
ActiveX已经做好了,下面开始调用
5.新建一个web项目,新建一个页面,把
-
-
-
-
-
-
- <script>
- function test() {
- var obj = document.getElementById("Object2"); var v=obj.Test("小花");//Test就是控件里面的方法,这里的大小写可以忽略 alert(v); var p=obj.UID;//UID就是控件里面定义的属性 }
- </script>
- <div>
- <input type="button" value="Test" οnclick="test()" />
- <object id="Object2" classid="clsid:0D69FA27-592D-4B7E-BF7D-A0BCDC54D1BC" width="100"
- height="25" style="display:none"
- codebase="/lib/setup.exe">
- </object>
- </div>
-
-
-
-
直接运行下,就可以了!
其他参考资料:http://msdn.microsoft.com/zh-cn/library/cc468260%28v=vs.71%29.aspx
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。