首先將可能會用到的WinScard指令先Import進來:
[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);
[DllImport("WinScard.dll")]
public static extern int SCardTransmit(int hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, ref SCARD_IO_REQUEST pioRecvPci, ref byte[] pbRecvBuffer, ref int pcbRecvLength);
[DllImport("WinScard.dll")]
public static extern int SCardTransmit(int hCard, ref SCARD_IO_REQUEST pioSendPci, byte[] pbSendBuffer, int cbSendLength, ref SCARD_IO_REQUEST pioRecvPci, ref byte pbRecvBuffer, ref int pcbRecvLength);
依序下指令:
SCardEstablishContext:建立環境。
SCardListReaders:讀取本機的讀卡機。
SCardConnect:讀卡機連線。
SCardTransmit:下指令給讀卡機並讀取讀卡機的回覆值。