当前位置:   article > 正文

VS2010创建ActiveX控件并在html中调用_activex在html里面加载方式

activex在html里面加载方式

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;

  1. [Guid("0D69FA27-592D-4B7E-BF7D-A0BCDC54D1BC")]//直接创建一个就可以,或用工具,点创建GUID,再复制过来
  2. public partial class TestUc : UserControl,IObjectSafety
  3. {
  4. public string UID { get; set; }
  5. public TestUc()
  6. {
  7. InitializeComponent();
  8. }
  9. public string Test(string abc)
  10. {
  11. UID = abc;
  12. return "TestUc:This is a " + abc;
  13. }
  14. #region IObjectSafety 成员
  15. private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
  16. private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
  17. private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
  18. private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
  19. private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
  20. private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
  21. private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
  22. private const int S_OK = 0;
  23. private const int E_FAIL = unchecked((int)0x80004005);
  24. private const int E_NOINTERFACE = unchecked((int)0x80004002);
  25. private bool _fSafeForScripting = true;
  26. private bool _fSafeForInitializing = true;
  27. public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
  28. {
  29. int Rslt = E_FAIL;
  30. string strGUID = riid.ToString("B");
  31. pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
  32. switch (strGUID)
  33. {
  34. case _IID_IDispatch:
  35. case _IID_IDispatchEx:
  36. Rslt = S_OK;
  37. pdwEnabledOptions = 0;
  38. if (_fSafeForScripting == true)
  39. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
  40. break;
  41. case _IID_IPersistStorage:
  42. case _IID_IPersistStream:
  43. case _IID_IPersistPropertyBag:
  44. Rslt = S_OK;
  45. pdwEnabledOptions = 0;
  46. if (_fSafeForInitializing == true)
  47. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
  48. break;
  49. default:
  50. Rslt = E_NOINTERFACE;
  51. break;
  52. }
  53. return Rslt;
  54. }
  55. public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
  56. {
  57. int Rslt = E_FAIL;
  58. string strGUID = riid.ToString("B");
  59. switch (strGUID)
  60. {
  61. case _IID_IDispatch:
  62. case _IID_IDispatchEx:
  63. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
  64. Rslt = S_OK;
  65. break;
  66. case _IID_IPersistStorage:
  67. case _IID_IPersistStream:
  68. case _IID_IPersistPropertyBag:
  69. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
  70. Rslt = S_OK;
  71. break;
  72. default:
  73. Rslt = E_NOINTERFACE;
  74. break;
  75. }
  76. return Rslt;
  77. }
  78. #endregion
  79. }

4.创建安装程序

在该解决方案上继续添加新工程,选择其他项目类型,选择安装工程

然后设置下属性Register

ActiveX已经做好了,下面开始调用


5.新建一个web项目,新建一个页面,把

  1. <script>
  2. function test() {
  3. var obj = document.getElementById("Object2"); var v=obj.Test("小花");//Test就是控件里面的方法,这里的大小写可以忽略 alert(v); var p=obj.UID;//UID就是控件里面定义的属性 }
  4. </script>
  5. <div>
  6. <input type="button" value="Test" οnclick="test()" />
  7. <object id="Object2" classid="clsid:0D69FA27-592D-4B7E-BF7D-A0BCDC54D1BC" width="100"
  8. height="25" style="display:none"
  9. codebase="/lib/setup.exe">
  10. </object>
  11. </div>

直接运行下,就可以了!

其他参考资料:http://msdn.microsoft.com/zh-cn/library/cc468260%28v=vs.71%29.aspx

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/161473
推荐阅读
相关标签
  

闽ICP备14008679号