visual studio 2012 的製做ActiveX、打包和發佈

開發環境是Vs 2012  Framework 4.0html

源碼和製做工具在文章最下邊web

一. ActiveX控件Demo安全

新建一個Window窗體控件庫項目app

在自動生成的UserControl1頁面上添加一個button工具

點擊事件裏咱們只彈出一個MesageBox網站

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click!!!!");
        }

IE怎麼知道一個插件是腳本安全的?它是經過如下兩個辦法。一是查詢ActiveX組件是否實現了IObjectSafety接口,而且ui

返回腳本安全;二是查詢ActiveX組件是否在註冊表的Component  Category Manager裏代表本身實現了spa

CATID_SafeForInitializing和CATID_SafeForScripting。插件

這裏咱們只說第一種實現IObjectSafety接口debug

接口格式是固定的不要修改

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);
    }
}

在UserControl1控件的後臺代碼咱們要實現這個接口

看一下代碼

using System;
using System.Windows.Forms;

namespace ActiveXDemo
{
    using System.Runtime.InteropServices;

    [Guid("6169E98E-DA08-4E87-81B6-EE3A5034C0E2"), 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("Click!!!!");
        }
    }
}

這裏要作一下修改

[Guid("6169E98E-DA08-4E87-81B6-EE3A5034C0E2"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]

這個guid是我本身生成的還有ProgId裏的ActiveXDemo.UserControl1是我本身工程裏的,修改爲你本身的
gui生成是在 菜單 工具 下邊

           

打開項目屬性,

在應用程序裏 點擊程序集信息 勾選 使程序集COM可見

在生成裏邊,勾選  爲Com互操做註冊

 

作到這裏咱們的ActiveX控件就作完了接下來咱們把它發佈

先是把它打包

Vs2012 打包我以前有些不一樣

新項目——其餘項目 安裝和部署

選擇application information:基本配置,自定義填寫

 接下來先把Application Files,點擊MyCompany下的第一個節點能夠本身重命名

 點擊 Add Project OutPuts

選擇主輸出點ok

由於咱們的demo就一個dll因此不用再有別的操做

若是想修改一下安裝的窗體能夠在Installation Interview裏設置

在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放到文件夾裏

在首頁上這樣寫

<body>
    <form id="form1" runat="server">
    <div>
    <object id="mytt" classid="clsid:6169E98E-DA08-4E87-81B6-EE3A5034C0E2"  
        codebase="/DLL/test.cab"></object>
    </div>
    </form>
</body>
</html>

這個Classid就是咱們在UserControl1.cs的Guid裏的序列號
看一下效果吧

 Active源碼.rar

cab製做工具.rar

相關文章
相關標籤/搜索