VS2012下制作ActiveX控件并添加到网页

为了节省时间,大部分资源取自http://www.cnblogs.com/li-peng/p/3455247.html,感谢作者。

流程概览

1.创建ActiveX控件——按钮

2.定义一个接口,并在控件中实现

3.部署安装

4.CAB打包

5.添加到网页中进行测试

 

一. 创建ActiveX控件——按钮

1.新建一个Window窗体控件库项目,命名为ActiveXDemo.

 

 

2.在自动生成的UserControl1页面上添加一个button

 

 

3.点击事件里我们只弹出一个MesageBox

 

 
  1. private void button1_Click(object sender, EventArgs e)

  2. {

  3. MessageBox.Show("Active is working now!");

  4. }

4.在右边解决方案资源管理器的ActiveXDemo项目上右键→属性→应用程序→程序集信息→使程序集COM可见(M)。再切换到“生成”标签→勾选“为COM互操作注册”。

 

 

 

 

 

 

 

5.打开Properties里面的AssemblyInof.cs文件,添加如下代码:

 

 

6.为控件创建GUID:工具→创建GUID,选5,点击复制

 

 

7.打开UserControl1.cs,在public partial class UserControl1 : UserControl上面粘贴上一步生成的GUID,并添加using System.Runtime.InteropServices;

代码如下:

 

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Drawing;

  5. using System.Data;

  6. using System.Linq;

  7. using System.Text;

  8. using System.Threading.Tasks;

  9. using System.Windows.Forms;

  10. using System.Runtime.InteropServices;

  11.  
  12. namespace ActiveXDemo

  13. {

  14. [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6")]

  15. public partial class UserControl1: UserControl

  16. {

  17. public UserControl1()

  18. {

  19. InitializeComponent();

  20. }

  21.  
  22. private void button1_Click(object sender, EventArgs e)

  23. {

  24. MessageBox.Show("ActiveX is working now!");

  25. }

  26. }

  27. }

 

 

二、定义一个接口,并在控件中实现

创建一个IObjectSafety接口,让ActiveX 控件获取客户端的信任。

1.右键ActiveXDemo项目—>添加—>新建项→Visual C#项→接口

注意接口内容是固定的不要修改!!!也就是说你直接复制粘贴就可以用了,不要管里面的序列号,跟上面生成的GUID不是一回事。

 

 
  1. using System;

  2. using System.Runtime.InteropServices;

  3. namespace ActiveXDemo

  4. {

  5. [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]

  6. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]

  7. public interface IObjectSafety

  8. {

  9. [PreserveSig]

  10. int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

  11.  
  12. [PreserveSig()]

  13. int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);

  14. }

  15. }

 

 

2.在UserControl1控件的后台代码UserControl1.cs中实现这个接口,代码如下:

 

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Drawing;

  5. using System.Data;

  6. using System.Linq;

  7. using System.Text;

  8. using System.Threading.Tasks;

  9. using System.Windows.Forms;

  10. using System.Runtime.InteropServices;

  11.  
  12. namespace ActiveXDemo

  13. {

  14. [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]

  15. public partial class UserControl1 : UserControl, IObjectSafety

  16. {

  17.  
  18. #region IObjectSafety 成员 格式固定

  19.  
  20. private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";

  21. private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";

  22. private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";

  23. private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";

  24. private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

  25.  
  26. private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;

  27. private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;

  28. private const int S_OK = 0;

  29. private const int E_FAIL = unchecked((int)0x80004005);

  30. private const int E_NOINTERFACE = unchecked((int)0x80004002);

  31.  
  32. private bool _fSafeForScripting = true;

  33. private bool _fSafeForInitializing = true;

  34.  
  35. public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)

  36. {

  37. int Rslt = E_FAIL;

  38.  
  39. string strGUID = riid.ToString("B");

  40. pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;

  41. switch (strGUID)

  42. {

  43. case _IID_IDispatch:

  44. case _IID_IDispatchEx:

  45. Rslt = S_OK;

  46. pdwEnabledOptions = 0;

  47. if (_fSafeForScripting == true)

  48. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;

  49. break;

  50. case _IID_IPersistStorage:

  51. case _IID_IPersistStream:

  52. case _IID_IPersistPropertyBag:

  53. Rslt = S_OK;

  54. pdwEnabledOptions = 0;

  55. if (_fSafeForInitializing == true)

  56. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;

  57. break;

  58. default:

  59. Rslt = E_NOINTERFACE;

  60. break;

  61. }

  62.  
  63. return Rslt;

  64. }

  65.  
  66. public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)

  67. {

  68. int Rslt = E_FAIL;

  69. string strGUID = riid.ToString("B");

  70. switch (strGUID)

  71. {

  72. case _IID_IDispatch:

  73. case _IID_IDispatchEx:

  74. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))

  75. Rslt = S_OK;

  76. break;

  77. case _IID_IPersistStorage:

  78. case _IID_IPersistStream:

  79. case _IID_IPersistPropertyBag:

  80. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))

  81. Rslt = S_OK;

  82. break;

  83. default:

  84. Rslt = E_NOINTERFACE;

  85. break;

  86. }

  87.  
  88. return Rslt;

  89. }

  90.  
  91. #endregion

  92. public UserControl1()

  93. {

  94. InitializeComponent();

  95. }

  96.  
  97. private void button1_Click(object sender, EventArgs e)

  98. {

  99. MessageBox.Show("ActiveX is working now!");

  100.  
  101. }

  102. }

  103. }


注意

[Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6")]

 

变成了

 

[Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]

 

 

可能发生的问题
到目前为止,ActiveXDemo项目完工了,ActiveXDemo项目右键-->生成。

这个时候,我这边出现了一个错误:

错误 1 无法注销程序集“D:\learnActiveX\ActiveXDemo\ActiveXDemo\bin\Debug\ActiveXDemo.dll”- 拒绝访问。请确保您正在以管理员身份运行应用程序。不允许所请求的注册表访问权。ActiveXDemo

最后发现用管理员身份运行VS2012,然后打开ActiveXDemo项目,再生成,就不会报错了。

 

三、打包部署

1.解决方案ActiveXDemo右键-->添加新项目-->其他项目类型-->安装和部署。如果你没有安装过这个,请先安装。命名为ActiveSetup

 

 

2.选择application information:基本配置,自定义填写

 3.接下来先把Application Files,点击MyCompany下的第一个节点可以自己重命名

 

 

 4.点击 Add Project OutPuts,选择主输出点ok

 

 

5.ActiveSetup项目右键,生成。在ActiveSetup\Express\DVD-5\DiskImages\DISK1文件夹下有如下文件:

 

四、使用cabarc.exe打包

1.去网上下载cabarc.exe,拷贝到ActiveSetup\Express\DVD-5\DiskImages\DISK1下。

2.在ActiveSetup\Express\DVD-5\DiskImages\DISK1下新建一个install.inf文件,内容如下:

 

 
  1. [version]

  2. signature="$CHICAGO$"

  3. AdvancedINF=2.0

  4.  
  5. [Setup Hooks]

  6. hook1=hook1

  7.  
  8. [hook1]

  9. run=msiexec.exe /i "%EXTRACT_DIR%\ActiveXSetup.msi" /qn

3.在同目录下创建一个批处理文件build.bat 

"cabarc.exe"  n test.cab ActiveXSetup.msi install.inf 

4.双击build.bat ,会自动生成一个test.cab。

 

五、添加到网页中测试
1.解决方案ActiveXDemo右键-->添加新项目-->Visual C#-->web-->ASP.NET Web窗体应用程序

 

2.WebApplication1项目右键-->添加新项-->Visual C#-->web-->Web窗体

 

3.打开WebForm1.aspx,在<div></div>之间添加上面做好的控件,代码如下

 

 
  1. <body>

  2. <form id="form1" runat="server">

  3. <div>

  4. <object id="mytt" classid="clsid:73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"

  5. codebase="/test.cab"></object>

  6. </div>

  7. </form>

  8. </body>

  9. </html>

clsid就是上面UserControl1.cs中的GUID

将上面的生成的test.cab添加到WebApplication1\bin\目录下
4.WebForm1.aspx右键设为起始页,WebApplication1项目右键设为启动项目

5.选择IE启动

 

可能遇到的问题

IE上显示的页面什么都没有,是由于ActiveX控件被拦截了。

在Ineternet选项中进行设置,确认“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”项设置为“启用”,“下载未签名的ActiveX控件”项设置为“提示”

 

 

在VS中再来启动项目,效果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值