1. 程式人生 > >C# 制作ActiveX控件並添加到網頁

C# 制作ActiveX控件並添加到網頁

-h edi lib border obj col facetype control article

1.創建ActiveX控件——按鈕

2.定義一個接口,並在控件中實現

3.部署安裝

4.CAB打包

5.添加到網頁中進行測試


一. 創建ActiveX控件——按鈕

1.新建一個Window窗體控件庫項目,命名為ActiveXDemo.

技術分享

2.在自動生成的UserControl1頁面上添加一個button

技術分享

3.點擊事件裏我們只彈出一個MesageBox

[csharp] view plain copy
  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;

代碼如下:

[csharp] view plain copy
  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. namespace ActiveXDemo
  12. {
  13. [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6")]
  14. public partial class UserControl1: UserControl
  15. {
  16. public UserControl1()
  17. {
  18. InitializeComponent();
  19. }
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. MessageBox.Show("ActiveX is working now!");
  23. }
  24. }
  25. }


二、定義一個接口,並在控件中實現

創建一個IObjectSafety接口,讓ActiveX 控件獲取客戶端的信任。

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

註意接口內容是固定的不要修改!!!也就是說你直接復制粘貼就可以用了,不要管裏面的序列號,跟上面生成的GUID不是一回事。

[csharp] view plain copy
  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. [PreserveSig()]
  12. int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
  13. }
  14. }


2.在UserControl1控件的後臺代碼UserControl1.cs中實現這個接口,代碼如下:

[csharp] view plain copy
  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. namespace ActiveXDemo
  12. {
  13. [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]
  14. public partial class UserControl1 : UserControl, IObjectSafety
  15. {
  16. #region IObjectSafety 成員 格式固定
  17. private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
  18. private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
  19. private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
  20. private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
  21. private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";
  22. private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
  23. private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
  24. private const int S_OK = 0;
  25. private const int E_FAIL = unchecked((int)0x80004005);
  26. private const int E_NOINTERFACE = unchecked((int)0x80004002);
  27. private bool _fSafeForScripting = true;
  28. private bool _fSafeForInitializing = true;
  29. public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
  30. {
  31. int Rslt = E_FAIL;
  32. string strGUID = riid.ToString("B");
  33. pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
  34. switch (strGUID)
  35. {
  36. case _IID_IDispatch:
  37. case _IID_IDispatchEx:
  38. Rslt = S_OK;
  39. pdwEnabledOptions = 0;
  40. if (_fSafeForScripting == true)
  41. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
  42. break;
  43. case _IID_IPersistStorage:
  44. case _IID_IPersistStream:
  45. case _IID_IPersistPropertyBag:
  46. Rslt = S_OK;
  47. pdwEnabledOptions = 0;
  48. if (_fSafeForInitializing == true)
  49. pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
  50. break;
  51. default:
  52. Rslt = E_NOINTERFACE;
  53. break;
  54. }
  55. return Rslt;
  56. }
  57. public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
  58. {
  59. int Rslt = E_FAIL;
  60. string strGUID = riid.ToString("B");
  61. switch (strGUID)
  62. {
  63. case _IID_IDispatch:
  64. case _IID_IDispatchEx:
  65. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
  66. Rslt = S_OK;
  67. break;
  68. case _IID_IPersistStorage:
  69. case _IID_IPersistStream:
  70. case _IID_IPersistPropertyBag:
  71. if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
  72. Rslt = S_OK;
  73. break;
  74. default:
  75. Rslt = E_NOINTERFACE;
  76. break;
  77. }
  78. return Rslt;
  79. }
  80. #endregion
  81. public UserControl1()
  82. {
  83. InitializeComponent();
  84. }
  85. private void button1_Click(object sender, EventArgs e)
  86. {
  87. MessageBox.Show("ActiveX is working now!");
  88. }
  89. }
  90. }


註意

[csharp] view plain copy
  1. [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6")]

變成了

[csharp] view plain copy
  1. [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文件夾下有如下文件:

一個exe一個mis我們只用mis的

還要一個cabarc.exe這個我會在下載裏給大家提供

 cabarc.exe:微軟提供的cab打包工具

ActiveXSetup.msi: 項目生成的部署安裝文件 install.inf : 需要跟ActiveXSetup.msi打包在一起的文件 build.bat: 打包的批處理命令

install.inf

技術分享
[version]
signature="$CHICAGO$"
AdvancedINF=2.0

[Setup Hooks]
hook1=hook1

[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\ActiveXSetup.msi" /qn
技術分享

build.bat

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

運行一下bat就可以生成一個
cab

我們隨便建一個webfrom網站

在debug下邊建一下dll文件夾

把test.cab放到文件夾裏

cabarc.exe:微軟提供的cab打包工具 地址 :files.cnblogs.com/files/dongh/cab%E5%88%B6%E4%BD%9C%E5%B7%A5%E5%85%B7.rar

C# 制作ActiveX控件並添加到網頁