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

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Active is working now!");
        }
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;

代碼如下:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Drawing;  
using System.Data;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using System.Runtime.InteropServices;  
  
namespace ActiveXDemo  
{  
    [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6")]  
    public partial class UserControl1: UserControl  
    {  
        public UserControl1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            MessageBox.Show("ActiveX is working now!");  
        }  
    }  
} 


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

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

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

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

using System;
using System.Runtime.InteropServices;
namespace ActiveXDemo
{   
    [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);
    }
}

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ActiveXDemo
{
    [Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]
    public partial class UserControl1 : UserControl, IObjectSafety
    {

        #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
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("ActiveX is working now!");

        }
    }
}

注意
[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文件,內容如下:

[version]
signature="$CHICAGO$"
AdvancedINF=2.0

[Setup Hooks]
hook1=hook1

[hook1]
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>之間添加上面做好的控件,代碼如下

<body>
    <form id="form1" runat="server">
    <div>
    <object id="mytt" classid="clsid:73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"  
        codebase="/test.cab"></object>
    </div>
    </form>
</body>
</html>
clsid就是上面UserControl1.cs中的GUID

將上面的生成的test.cab添加到WebApplication1\bin\目錄下
4.WebForm1.aspx右鍵設爲起始頁,WebApplication1項目右鍵設爲啓動項目

5.選擇IE啓動


可能遇到的問題

IE上顯示的頁面什麼都沒有,是由於ActiveX控件被攔截了。

在Ineternet選項中進行設置,確認“對未標記爲可安全執行腳本的ActiveX控件初始化並執行腳本”項設置爲“啓用”,“下載未簽名的ActiveX控件”項設置爲“提示”



在VS中再來啓動項目,效果如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章