C#開發Activex控制項(二)

來源:互聯網
上載者:User

今天就開始製作Activex外掛程式。

1

2.暫時放個文字框,和按鈕,點擊按鈕後將所要顯示資訊顯示到文字框中

3. 更改“項目屬性-應用程式-程式集資訊”設定,勾選“使程式集 COM 可見”(重要):

4.更改“項目屬性-產生”設定,勾選“為 COM Interop 註冊”(注意,此處如果實在debug狀態下修改的,那在調到release狀態下還需要再設定一次)(重要):

5.修改AssemblyInfo.cs檔案,添加[assembly: AllowPartiallyTrustedCallers()]項(需要引用System.Security名稱空間):

6.下面就開始準備核心部分,首先,為控制項類添加GUID,這個編號將用於B/S系統的用戶端調用時使用(可以在SDK工具-建立GUID ):

7.  為了讓ActiveX控制項獲得用戶端的信任,控制項類還需要實現一個名為“IObjectSafety”的介面。先建立該介面(注意,不能修改該介面的GUID值):

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices; namespace CardReaderControl{    [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);    }}

8.然後在控制項類中繼承並實現該介面: 

View Code

 #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

9.然後就可以寫自己的代碼了,再次直接點擊按鈕,然後刷卡,輸出資料,這塊代碼只是測試代碼,還沒進行很好的整理

View Code

 private void btn_send_Click(object sender, EventArgs e)        {            try            {                serialPort1.PortName = "COM1";//連接埠號碼                serialPort1.BaudRate = 9600;//傳輸速率                serialPort1.Parity = 0;//無同位                //serialPort1.StopBits = (StopBits)Convert.ToByte("1", 10);                serialPort1.DataBits = 8;//資料位元                serialPort1.Open();                //txtShow.AppendText("初始化串口成功!");                byte[] data = new byte[2]; //mysendb();                data[0] = 0x1b;                data[1] = 0x5d;                serialPort1.Write(data, 0, 2);                // serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);//注此事件監聽串口是否有資料                try                {                    Thread.Sleep(2000);//此處暫時延時實現效果 本應該監聽串口處是否有資料,有的話取出,否則一直等待                    int bytes = serialPort1.BytesToRead;                    // 建立位元組數組                    byte[] buffer = new byte[bytes];                    // 讀取緩衝區的資料到數組                    serialPort1.Read(buffer, 0, bytes);                    string str = Encoding.ASCII.GetString(buffer);//將二進位轉換成字元                    string endStr = str.Substring(2, str.Length - 4);                    //txtShow.AppendText(endStr);                    txtShow.Text = endStr;                    serialPort1.Close();                }                catch (Exception ex)                {                    serialPort1.Close();                    MessageBox.Show(ex.Message);                }            }            catch            {                MessageBox.Show("初始化串口失敗!");                //txtShow.AppendText("初始化串口失敗!");            }        }

下面就要進行打包處理,然後調用,本月實在太忙,只有抽下班那麼一會(悲催的程式猿),簡單寫點,其餘部分會在下面幾篇寫出!今天的就當準備工作吧!

由於時間問題,直接源碼:源碼

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.