首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 開發 ActiveX 實現 Web 端 RFID 讀卡功能完整指南

     編輯:關於C語言

專案背景與技術選型

在 Web 專案中整合 RFID 讀卡器,傳統方案多採用 VC++ 或 VB6 開發 ActiveX 控制項。考慮到團隊對 C# 技術棧的熟悉度,本文採用 Windows Forms Control → COM Interop → ActiveX 的路徑,以 C# 快速構建可在 IE 中運行的 RFID 讀卡元件。

核心步驟概覽

  1. 建立 Windows Forms 控制項專案
  2. 實作 COM 可見介面與安全性標記
  3. 簽署組件、註冊 COM Interop、產生強名稱金鑰
  4. 製作 MSI 安裝包並轉換為 CAB 簽章檔
  5. 網頁中透過 <object> 標籤引用

關鍵程式碼實作

1. 底層 DLL 互操作封裝

針對廠商提供的 MasterRD.dll 進行 P/Invoke 宣告:

using System;
using System.Runtime.InteropServices;

namespace RFIDReader {
    public class ReadRfid {
        [DllImport("MasterRD.dll")] private static extern int rf_init_com(int port, int baud);
        [DllImport("MasterRD.dll")] private static extern int rf_request(short icdev, byte model, ref short TagType);
        [DllImport("MasterRD.dll")] private static extern int rf_anticoll(short icdev, byte bcnt, ref byte PPSnr, ref byte pRLength);
        [DllImport("MasterRD.dll")] private static extern int rf_ClosePort();

        public string CardNum {
            get { return getCardNum(); }
        }

        private string getCardNum() {
            int port = 4, baud = 9600; // COM4, 9600bps
            byte model = 0x52; short tagType = 0; byte bcnt = 4;
            byte[] buf = new byte[200];
            try {
                rf_init_com(port, baud);
                rf_request(0, model, ref tagType);
                rf_anticoll(0, bcnt, ref buf[0], ref bcnt);
                rf_ClosePort();
                string sn = "";
                for (int i = 0; i < bcnt; i++) sn += Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0');
                if (sn == "0000") throw new Exception("讀取失敗");
                return sn;
            } catch { return ""; }
        }
    }
}

2. COM 可見控制項與安全性介面

實作 IObjectSafety 讓 IE 信任控制項可安全載入:

[ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety {
    [PreserveSig] void GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
    [PreserveSig] void SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}

[Guid("0CBD6597-3953-4B88-8C9F-F58B1B023413"),
 ProgId("RFIDReader.Reader"),
 ComVisible(true)]
public partial class Reader : UserControl, IObjectSafety {
    private Timer timer1 = new Timer();
    private ReadRfid rfid = new ReadRfid();

    public Reader() { InitializeComponent(); timer1.Tick += Timer1_Tick; }

    public int TimerSpan { get => timer1.Interval; set => timer1.Interval = value; }
    public string CardNum { get => textBox1.Text; }

    public void GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions) {
        pdwSupportedOptions = 1; pdwEnabledOptions = 2; // INTERFACESAFE_FOR_UNTRUSTED_CALLER/ DATA
    }
    public void SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { /* 不拋異常即視為信任 */ }

    private void Timer1_Tick(object sender, EventArgs e) {
        string sn = rfid.CardNum;
        if (!string.IsNullOrEmpty(sn)) { textBox1.Text = sn; GetInfo(); }
    }
    private void GetInfo() { label1.Text = "卡片已讀取"; }
}

3. 組件層級設定

在 AssemblyInfo.cs 加入:

[assembly: AllowPartiallyTrustedCallers()]
[assembly: ComVisible(true)]

專案屬性 → Build → 勾選 Register for COM interop;並簽署強名稱金鑰(.snk)。

部署流程:從 MSI 到 CAB

  1. 新增 Setup Project,加入主輸出
  2. 建置產生 ReaderInstaller.msi
  3. 編寫 install.inf:
[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\ReaderInstaller.msi" /qn
  1. 使用 cabarc.exe 打包:cabarc n RFIDReader.cab ReaderInstaller.msi install.inf
  2. 簽署 CAB 檔(需代碼簽章憑證)

網頁端引用與常見問題

<object id="RFIDReader" classid="clsid:0CBD6597-3953-4B88-8C9F-F58B1B023413"
    codebase="RFID/RFIDReader.cab"></object>
  • classid 必須與 GuidAttribute 一致
  • 客戶端需將網站加入 IE「受信任站點」,並啟用「對未標記為安全的 ActiveX 初始化並指令碼執行」
  • 若無法自動下載 CAB,需手動執行 MSI 安裝
  • COM 埠號在實際部署時建議改為可配置參數,避免硬編碼

原文參考

C#用Activex實現Web客戶端讀取RFID功能

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved