使用C#呼叫Winscard.DLL對Smart Card應用 |
|
sockman
一般會員 發表:24 回覆:29 積分:10 註冊:2003-03-17 發送簡訊給我 |
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices; //使用外部DLL時使用 namespace MikeYeager.SmartCard.Main
{
public class SmartCard_PCSC : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null; private int nContext; //卡機context handle
private int nCard; //卡片連線 handle
private int nActiveProtocol; //T0, T1, etc... //Smart Card PCSC dll functions (windows)
[DllImport("WinScard.dll")]
public static extern int SCardEstablishContext(uint dwScope, int nNotUsed1,
int nNotUsed2, ref int phContext);
[DllImport("WinScard.dll")]
public static extern int SCardReleaseContext(int phContext);
[DllImport("WinScard.dll")]
public static extern int SCardConnect(int hContext, string cReaderName,
uint dwShareMode, uint dwPrefProtocol, ref int phCard, ref int ActiveProtocol);
[DllImport("WinScard.dll")]
public static extern int SCardDisconnect(int hCard, int Disposition);
[DllImport("WinScard.dll")]
public static extern int SCardListReaderGroups(int hContext, ref string cGroups, ref int nStringSize);
[DllImport("WinScard.dll")]
public static extern int SCardListReaders(int hContext, string cGroups,
ref string cReaderLists, ref int nReaderCount);
[DllImport("WinScard.dll")]
public static extern int SCardFreeMemory(int hContext, string cResourceToFree);
[DllImport("WinScard.dll")]
public static extern int SCardGetAttrib(int hContext, uint dwAttrId,
ref byte[] bytRecvAttr, ref int nRecLen); public SmartCard_PCSC()
{
InitializeComponent(); //First step in using smart cards is CSardEstablishContext()
uint nContext = 2; //system
int nNotUsed1 = 0;
int nNotUsed2 = 0;
this.nContext = 0; //handle to context for future calls to use int nRetVal1 = SCardEstablishContext(nContext, nNotUsed1, nNotUsed2, ref this.nContext);
if (nRetVal1 != 0)
{
DisplaySCError(nRetVal1, "SCardEstablishContext()");
return;
} //used to split null delimited strings into string arrays
char[] delimiter = new char[1];
delimiter[0] = Convert.ToChar(0); //Second step in using smart cards is SCardListReaderGroups() to determin reader to use
string cGroupList = "" Convert.ToChar(0);
int nStringSize = -1; //SCARD_AUTOALLOCATE
int nRetVal2 = SCardListReaderGroups(this.nContext, ref cGroupList, ref nStringSize);
if (nRetVal2 != 0)
{
DisplaySCError(nRetVal2,"SCardListReaderGroups()");
return;
} string[] cGroups = cGroupList.Split(delimiter);
/*foreach (string cName in cGroups)
{
MessageBox.Show("Reader Group: " cName);
}*/ /*int nRetVal3 = SCardFreeMemory(this.nContext, ref cGroupList);
if (nRetVal3 != 0)
{
DisplaySCError(nRetVal3,"SCardFreeMemory()");
return;
}*/ string cReaderList = "" Convert.ToChar(0);
int nReaderCount = -1;
int nRetVal4 = SCardListReaders(this.nContext, cGroups[0], ref cReaderList, ref nReaderCount);
if (nRetVal4 != 0)
{
DisplaySCError(nRetVal4,"SCardListReaders()");
return;
} string[] cReaders = cReaderList.Split(delimiter);
/*foreach (string cName in cReaders)
{
MessageBox.Show("Reader: " cName);
}*/ /*int nRetVal5 = SCardFreeMemory(this.nContext, ref cReaderList);
if (nRetVal5 != 0)
{
DisplaySCError(nRetVal5,"SCardFreeMemory()");
return;
}*/ uint nShareMode = 1; //exclusive
uint nPrefProtocol = 0x80000000; //default PTS
this.nCard = 0;
this.nActiveProtocol = 0;
int nRetVal6 = SCardConnect(this.nContext, cReaders[0], nShareMode, nPrefProtocol, ref this.nCard, ref this.nActiveProtocol);
if (nRetVal6 != 0)
DisplaySCError(nRetVal6,"SCardConnect()");
} protected override void Dispose( bool disposing )
{
if( disposing )
{
int nDisposition = 0; //don't do power down, eject, etc...
int nRetVal = SCardDisconnect(this.nCard, nDisposition);
if (nRetVal != 0)
DisplaySCError(nRetVal); int nRetVal2 = SCardReleaseContext(this.nContext);
if (nRetVal2 != 0)
DisplaySCError(nRetVal2); if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
#endregion private void DisplaySCError(int nErrCode)
{
DisplaySCError(nErrCode, "");
} private void DisplaySCError(int nErrCode, string cText)
{
string cErrString="";
string ErrCode = String.Format("0x{0:X}", nErrCode); //例外的Error code處理
switch (ErrCode)
{
case "0x80100001":
cErrString="Internal Error";
break;
case "0x80100002":
cErrString="Cancelled";
break;
case "0x80100003":
cErrString="Invalid Handle";
break;
case "0x80100004":
cErrString="Invalid Parameter";
break;
case "0x80100005":
cErrString="Invalid Target";
break;
case "0x80100006":
cErrString="No Memory";
break;
case "0x80100007":
cErrString="Waited Too Long";
break;
case "0x80100008":
cErrString="Insufficient Buffer";
break;
case "0x80100009":
cErrString="Unknown Reader";
break;
case "0x8010000A":
cErrString="Timeout";
break;
case "0x8010000B":
cErrString="Sharing Violation";
break;
case "0x8010000C":
cErrString="No Smart Card";
break;
case "0x8010000D":
cErrString="Unknown Card";
break;
case "0x8010000E":
cErrString="Can't Dispose";
break;
case "0x8010000F":
cErrString="Proto Mismatch";
break;
case "0x80100010":
cErrString="Not Ready";
break;
case "0x80100011":
cErrString="Invalid Value";
break;
case "0x80100012":
cErrString="System Cancelled";
break;
case "0x80100013":
cErrString="Comm Error";
break;
case "0x80100014":
cErrString="Unknown Error";
break;
case "0x80100015":
cErrString="Invalid ATR";
break;
case "0x80100016":
cErrString="Not Transacted";
break;
case "0x80100017":
cErrString="Reader Unavailable";
break;
case "0x80100018":
cErrString="Shutdown";
break;
case "0x80100019":
cErrString="PCI Too Small";
break;
case "0x8010001A":
cErrString="Reader Unsupported";
break;
case "0x8010001B":
cErrString="Duplicate Reader";
break;
case "0x8010001C":
cErrString="Card Unsupported";
break;
case "0x8010001D":
cErrString="No Service";
break;
case "0x8010001E":
cErrString="Service Stopped";
break;
case "0x80100065":
cErrString="Unsupported Card";
break;
case "0x80100066":
cErrString="Unresponsive Card";
break;
case "0x80100067":
cErrString="Unpowered Card";
break;
case "0x80100068":
cErrString="Reset Card";
break;
case "0x80100069":
cErrString="Removed Card";
break;
default:
cErrString="Unknown PC/SC Error Code - No Further Information Is Available";
break;
} MessageBox.Show(String.Format("Error Code: 0x{0:X}\n\n" cErrString "\n\n" cText, nErrCode),
"Smart Card PC/SC Error");
} private string GetSCReaderInfo()
{
string cRetVal = "";
byte[] bytRecvAttr = new byte[256];
int nRecLen = 0;
int nResult = 0; //Get vendor name
nRecLen = 255;
nResult = SCardGetAttrib(this.nCard, 0x100, ref bytRecvAttr, ref nRecLen);
if (nResult != 0)
{
DisplaySCError(nResult);
MessageBox.Show("Failed to get Vendor Name");
}
else
cRetVal = "VendorName: " bytRecvAttr.ToString() "\n"; //Get vendor IFD type
nRecLen = 255;
nResult = SCardGetAttrib(this.nCard, 0x101, ref bytRecvAttr, ref nRecLen);
if (nResult != 0)
{
DisplaySCError(nResult);
MessageBox.Show("Failed to get IFD Type");
}
else
cRetVal = "IFD Type: " bytRecvAttr.ToString() "\n"; return cRetVal;
} private void mmReaderInfoButton_Click(object sender, System.EventArgs e)
{
MessageBox.Show(GetSCReaderInfo());
} }
}
|
rogerchao
一般會員 發表:28 回覆:12 積分:8 註冊:2005-07-27 發送簡訊給我 |
您好
我發現執行下面這一段會有錯誤, 請問您有run這支程式嗎?謝謝 uint nContext = 0; int nNotUsed1 = 0; int nNotUsed2 = 0; this.nContext = 0; int nRetVal1 = SCardEstablishContext(nContext, nNotUsed1, nNotUsed2, ref this.nContext);//20080509 if (nRetVal1 != 0) { DisplaySCError(nRetVal1, "SCardEstablishContext()"); return; } |
wee4071
一般會員 發表:0 回覆:1 積分:0 註冊:2009-01-16 發送簡訊給我 |
===================引 用 sockman 文 章=================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; //使用外部DLL時使用 namespace MikeYeager.SmartCard.Main { public class SmartCard_PCSC : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private int nContext; //卡機context handle private int nCard; //卡片連線 handle private int nActiveProtocol; //T0, T1, etc... //Smart Card PCSC dll functions (windows) [DllImport("WinScard.dll")] public static extern int SCardEstablishContext(uint dwScope, int nNotUsed1, int nNotUsed2, ref int phContext); [DllImport("WinScard.dll")] public static extern int SCardReleaseContext(int phContext); [DllImport("WinScard.dll")] public static extern int SCardConnect(int hContext, string cReaderName, uint dwShareMode, uint dwPrefProtocol, ref int phCard, ref int ActiveProtocol); [DllImport("WinScard.dll")] public static extern int SCardDisconnect(int hCard, int Disposition); [DllImport("WinScard.dll")] public static extern int SCardListReaderGroups(int hContext, ref string cGroups, ref int nStringSize); [DllImport("WinScard.dll")] public static extern int SCardListReaders(int hContext, string cGroups, ref string cReaderLists, ref int nReaderCount); [DllImport("WinScard.dll")] public static extern int SCardFreeMemory(int hContext, string cResourceToFree); [DllImport("WinScard.dll")] public static extern int SCardGetAttrib(int hContext, uint dwAttrId, ref byte[] bytRecvAttr, ref int nRecLen); public SmartCard_PCSC() { InitializeComponent(); //First step in using smart cards is CSardEstablishContext() uint nContext = 2; //system int nNotUsed1 = 0; int nNotUsed2 = 0; this.nContext = 0; //handle to context for future calls to use int nRetVal1 = SCardEstablishContext(nContext, nNotUsed1, nNotUsed2, ref this.nContext); if (nRetVal1 != 0) { DisplaySCError(nRetVal1, "SCardEstablishContext()"); return; } //used to split null delimited strings into string arrays char[] delimiter = new char[1]; delimiter[0] = Convert.ToChar(0); //Second step in using smart cards is SCardListReaderGroups() to determin reader to use string cGroupList = "" Convert.ToChar(0); int nStringSize = -1; //SCARD_AUTOALLOCATE int nRetVal2 = SCardListReaderGroups(this.nContext, ref cGroupList, ref nStringSize); if (nRetVal2 != 0) { DisplaySCError(nRetVal2,"SCardListReaderGroups()"); return; } string[] cGroups = cGroupList.Split(delimiter); /*foreach (string cName in cGroups) { MessageBox.Show("Reader Group: " cName); }*/ /*int nRetVal3 = SCardFreeMemory(this.nContext, ref cGroupList); if (nRetVal3 != 0) { DisplaySCError(nRetVal3,"SCardFreeMemory()"); return; }*/ string cReaderList = "" Convert.ToChar(0); int nReaderCount = -1; int nRetVal4 = SCardListReaders(this.nContext, cGroups[0], ref cReaderList, ref nReaderCount); if (nRetVal4 != 0) { DisplaySCError(nRetVal4,"SCardListReaders()"); return; } string[] cReaders = cReaderList.Split(delimiter); /*foreach (string cName in cReaders) { MessageBox.Show("Reader: " cName); }*/ /*int nRetVal5 = SCardFreeMemory(this.nContext, ref cReaderList); if (nRetVal5 != 0) { DisplaySCError(nRetVal5,"SCardFreeMemory()"); return; }*/ uint nShareMode = 1; //exclusive uint nPrefProtocol = 0x80000000; //default PTS this.nCard = 0; this.nActiveProtocol = 0; int nRetVal6 = SCardConnect(this.nContext, cReaders[0], nShareMode, nPrefProtocol, ref this.nCard, ref this.nActiveProtocol); if (nRetVal6 != 0) DisplaySCError(nRetVal6,"SCardConnect()"); } protected override void Dispose( bool disposing ) { if( disposing ) { int nDisposition = 0; //don't do power down, eject, etc... int nRetVal = SCardDisconnect(this.nCard, nDisposition); if (nRetVal != 0) DisplaySCError(nRetVal); int nRetVal2 = SCardReleaseContext(this.nContext); if (nRetVal2 != 0) DisplaySCError(nRetVal2); if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code #endregion private void DisplaySCError(int nErrCode) { DisplaySCError(nErrCode, ""); } private void DisplaySCError(int nErrCode, string cText) { string cErrString=""; string ErrCode = String.Format("0x{0:X}", nErrCode); //例外的Error code處理 switch (ErrCode) { case "0x80100001": cErrString="Internal Error"; break; case "0x80100002": cErrString="Cancelled"; break; case "0x80100003": cErrString="Invalid Handle"; break; case "0x80100004": cErrString="Invalid Parameter"; break; case "0x80100005": cErrString="Invalid Target"; break; case "0x80100006": cErrString="No Memory"; break; case "0x80100007": cErrString="Waited Too Long"; break; case "0x80100008": cErrString="Insufficient Buffer"; break; case "0x80100009": cErrString="Unknown Reader"; break; case "0x8010000A": cErrString="Timeout"; break; case "0x8010000B": cErrString="Sharing Violation"; break; case "0x8010000C": cErrString="No Smart Card"; break; case "0x8010000D": cErrString="Unknown Card"; break; case "0x8010000E": cErrString="Can't Dispose"; break; case "0x8010000F": cErrString="Proto Mismatch"; break; case "0x80100010": cErrString="Not Ready"; break; case "0x80100011": cErrString="Invalid Value"; break; case "0x80100012": cErrString="System Cancelled"; break; case "0x80100013": cErrString="Comm Error"; break; case "0x80100014": cErrString="Unknown Error"; break; case "0x80100015": cErrString="Invalid ATR"; break; case "0x80100016": cErrString="Not Transacted"; break; case "0x80100017": cErrString="Reader Unavailable"; break; case "0x80100018": cErrString="Shutdown"; break; case "0x80100019": cErrString="PCI Too Small"; break; case "0x8010001A": cErrString="Reader Unsupported"; break; case "0x8010001B": cErrString="Duplicate Reader"; break; case "0x8010001C": cErrString="Card Unsupported"; break; case "0x8010001D": cErrString="No Service"; break; case "0x8010001E": cErrString="Service Stopped"; break; case "0x80100065": cErrString="Unsupported Card"; break; case "0x80100066": cErrString="Unresponsive Card"; break; case "0x80100067": cErrString="Unpowered Card"; break; case "0x80100068": cErrString="Reset Card"; break; case "0x80100069": cErrString="Removed Card"; break; default: cErrString="Unknown PC/SC Error Code - No Further Information Is Available"; break; } MessageBox.Show(String.Format("Error Code: 0x{0:X}\n\n" cErrString "\n\n" cText, nErrCode), "Smart Card PC/SC Error"); } private string GetSCReaderInfo() { string cRetVal = ""; byte[] bytRecvAttr = new byte[256]; int nRecLen = 0; int nResult = 0; //Get vendor name nRecLen = 255; nResult = SCardGetAttrib(this.nCard, 0x100, ref bytRecvAttr, ref nRecLen); if (nResult != 0) { DisplaySCError(nResult); MessageBox.Show("Failed to get Vendor Name"); } else cRetVal = "VendorName: " bytRecvAttr.ToString() "\n"; //Get vendor IFD type nRecLen = 255; nResult = SCardGetAttrib(this.nCard, 0x101, ref bytRecvAttr, ref nRecLen); if (nResult != 0) { DisplaySCError(nResult); MessageBox.Show("Failed to get IFD Type"); } else cRetVal = "IFD Type: " bytRecvAttr.ToString() "\n"; return cRetVal; } private void mmReaderInfoButton_Click(object sender, System.EventArgs e) { MessageBox.Show(GetSCReaderInfo()); } } } |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |