📄 t0demo.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using GrabbaDotNet;
namespace Grabba_T0_Demo_.NET
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class T0Demo : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.Label ATR;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem Exit;
private System.Windows.Forms.TextBox Data;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button RefreshButton;
private Button enableBtn;
Grabba myGrabba;
private Button testBtn;
public T0Demo()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
myGrabba = new Grabba();
if (myGrabba.Open() != myGrabba.errNone)
{
//problem opening grabba
MessageBox.Show("Unable to open grabba", "Open",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
Application.Exit();
}
Grabba.GrabbaModelType myGrabbaModel = new Grabba.GrabbaModelType();
myGrabba.GetModel(ref myGrabbaModel);
// check that the barcode device was detected as a T-3300 / T-4300
if (!(myGrabbaModel.smartCardDevice))
{
// failure do something here since it wasnt detected properly
MessageBox.Show("Not a smartcard device", "GetModel",
MessageBoxButtons.OK,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1);
Application.Exit();
}
// add event handler
Grabba.GrabbaSmartCardInsertionEvent += new EventHandler(onSmartCardInserted);
Grabba.GrabbaSmartCardRemovalEvent += new EventHandler(onSmartCardRemoved);
Grabba.GrabbaDisconnectedEvent += new EventHandler(onGrabbaDisconnectedEvent);
Grabba.GrabbaReconnectedEvent += new EventHandler(onGrabbaReconnectedEvent);
//RefreshDisplay();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
//SmartCard.Disable();
//Grabba.Close();
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.Exit = new System.Windows.Forms.MenuItem();
this.ATR = new System.Windows.Forms.Label();
this.Data = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.RefreshButton = new System.Windows.Forms.Button();
this.enableBtn = new System.Windows.Forms.Button();
this.testBtn = new System.Windows.Forms.Button();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menuItem1);
//
// menuItem1
//
this.menuItem1.MenuItems.Add(this.Exit);
this.menuItem1.Text = "Menu";
//
// Exit
//
this.Exit.Text = "Exit";
this.Exit.Click += new System.EventHandler(this.Exit_Click);
//
// ATR
//
this.ATR.Location = new System.Drawing.Point(40, 64);
this.ATR.Size = new System.Drawing.Size(168, 40);
this.ATR.Text = "ATR";
//
// Data
//
this.Data.Location = new System.Drawing.Point(82, 119);
this.Data.Size = new System.Drawing.Size(100, 21);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 120);
this.label1.Size = new System.Drawing.Size(32, 20);
this.label1.Text = "Data:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 40);
this.label2.Size = new System.Drawing.Size(32, 20);
this.label2.Text = "ATR:";
//
// RefreshButton
//
this.RefreshButton.Location = new System.Drawing.Point(144, 232);
this.RefreshButton.Size = new System.Drawing.Size(72, 20);
this.RefreshButton.Text = "Refresh";
this.RefreshButton.Click += new System.EventHandler(this.RefreshButton_Click);
//
// enableBtn
//
this.enableBtn.Location = new System.Drawing.Point(25, 231);
this.enableBtn.Size = new System.Drawing.Size(72, 20);
this.enableBtn.Text = "Enable";
this.enableBtn.Click += new System.EventHandler(this.enableBtn_Click);
//
// testBtn
//
this.testBtn.Location = new System.Drawing.Point(129, 179);
this.testBtn.Size = new System.Drawing.Size(87, 33);
this.testBtn.Text = "Test";
this.testBtn.Click += new System.EventHandler(this.testBtn_Click);
//
// T0Demo
//
this.ClientSize = new System.Drawing.Size(240, 268);
this.Controls.Add(this.testBtn);
this.Controls.Add(this.enableBtn);
this.Controls.Add(this.RefreshButton);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.Data);
this.Controls.Add(this.ATR);
this.Menu = this.mainMenu1;
this.Text = "T0 Demo";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new T0Demo());
}
private void Exit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void RefreshDisplay()
{
uint rc;
int i;
Grabba.SmartCardSession scSession = new Grabba.SmartCardSession();
scSession.cardType = Grabba.SmartCardType.SC_T0;
do
{
if(MessageBox.Show("Please insert card now.", "MESSAGE",
MessageBoxButtons.OKCancel,
MessageBoxIcon.None,
MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
return;
rc = myGrabba.SmartCardStartSession(ref scSession);
} while(rc.Equals(myGrabba.grabbaSmartCardErrNoCardInserted));
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Unable to access card. Error " + rc + ".", "ERROR");
return;
}
ATR.Text = "";
for(i = 0; i < scSession.ATRLen; i ++)
{
ATR.Text += scSession.ATR[i].ToString() + " ";
}
// Do a Select File command and select FF03 (Security Management File)
Grabba.SmartCardAPDU apdu = new Grabba.SmartCardAPDU();
apdu.CLA = 0x80;
apdu.INS = 0x20; // Submit code command
apdu.P1 = 0x07; // index for the IC code. Check the ACOS manual for index of other keys
apdu.P2 = 0x00;
apdu.Lc = 0x08; // Keys are 8 bytes long.
apdu.Le = 0x00;
// Hex of 'ACOSTEST' = 41 43 4F 53 54 45 53 54
apdu.dataIn[0] = 0x41;
apdu.dataIn[1] = 0x43;
apdu.dataIn[2] = 0x4F;
apdu.dataIn[3] = 0x53;
apdu.dataIn[4] = 0x54;
apdu.dataIn[5] = 0x45;
apdu.dataIn[6] = 0x53;
apdu.dataIn[7] = 0x54;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
{
MessageBox.Show("Card access failed. Error code 0x" + rc.ToString(), "ERROR");
return;
}
// Start the Session
apdu.CLA = 0x80;
apdu.INS = 0x84;
apdu.P1 = 0x00;
apdu.P2 = 0x00;
apdu.Lc = 0x00;
apdu.Le = 0x08;
rc = myGrabba.SmartCardExchangeAPDU(ref apdu);
if (!(rc.Equals(myGrabba.errNone)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -