/// <summary> /// カード接続 /// </summary> /// <param name="rwContext">RWコンテキスト</param> /// <param name="reader">リーダー名</param> /// <param name="shareMode">共有指定</param> /// <param name="protocol">プロトコル指定</param> /// <param name="card">[OUT]カード</param> /// <param name="activeProtocol">[OUT]接続プロトコル</param> /// <returns></returns> public static bool Connect(IntPtr rwContext, String reader, uint shareMode, uint protocol, ref IntPtr card, ref uint activeProtocol) { lastError = (WinSCardError)SCardConnect(rwContext, reader, shareMode, protocol, ref card, ref activeProtocol); Debug.WriteLine("WinSCard.Connect Res:" + lastError.ToString()); return(lastError == WinSCardError.SCARD_S_SUCCESS); }
/// <summary> /// カードコマンド送受信 /// </summary> /// <param name="card">カードコンテキスト</param> /// <param name="sendPci">GetPciを使用して取得したPCI構造体</param> /// <param name="sendBuffer">送信データ</param> /// <param name="sendLen">送信データ長</param> /// <param name="recvPci">レスポンス格納用PCI構造体</param> /// <param name="recvBuffer">レスポンス格納バッファ</param> /// <param name="recvLen">[OUT]レスポンス長</param> /// <returns></returns> public static bool Transmit(IntPtr card, SCARD_IO_REQUEST sendPci, byte[] sendBuffer, int sendLen, ref SCARD_IO_REQUEST recvPci, byte[] recvBuffer, ref uint recvLen) { Debug.WriteLine("WinSCard.Transmit Send:" + Utility.ByteToHex(sendBuffer, 0, (int)sendLen)); lastError = (WinSCardError)SCardTransmit( card, ref sendPci, sendBuffer, (uint)sendLen, ref recvPci, recvBuffer, ref recvLen); if (lastError == WinSCardError.SCARD_S_SUCCESS && recvBuffer[recvLen - 2] == 0x90 && recvBuffer[recvLen - 1] == 0x00) { Array.Copy(recvBuffer, recvLen - 2, lastRes, 0, lastRes.Length); Debug.WriteLine("WinSCard.Transmit OK:" + Utility.ByteToHex(recvBuffer, 0, (int)recvLen)); return(true); } else if (lastError == WinSCardError.SCARD_S_SUCCESS) { Array.Copy(recvBuffer, recvLen - 2, lastRes, 0, lastRes.Length); Debug.WriteLine("WinSCard.Transmit Error:" + Utility.ByteToHex(recvBuffer, 0, (int)recvLen)); return(false); } else { Debug.WriteLine("WinSCard.Transmit Error:" + lastError.ToString()); return(false); } }
/// <summary> /// リーダー名称取得 /// </summary> /// <param name="rwContext">RWコンテキスト</param> /// <returns></returns> public static string ListReaders(IntPtr rwContext) { uint len = 0; lastError = (WinSCardError)SCardListReaders(rwContext, null, null, ref len); if (lastError != WinSCardError.SCARD_S_SUCCESS) { return(null); } byte[] buffer = new byte[len]; lastError = (WinSCardError)SCardListReaders(rwContext, null, buffer, ref len); string readers = Encoding.ASCII.GetString(buffer, 0, (int)len); string[] names = readers.Split(new Char[] { '\0' }); foreach (string name in names) { if (name.IndexOf("PaSoRi", StringComparison.OrdinalIgnoreCase) >= 0) { return(name); } } return(null); }
/// <summary> /// ATR取得用 /// </summary> /// <param name="card">カードコンテキスト</param> /// <param name="state">[OUT]リーダー状態</param> /// <param name="protocol">[OUT]プロトコル</param> /// <param name="atr">[OUT]</param> /// <returns></returns> public static bool GetStatus(IntPtr card, out uint state, out uint protocol, out byte[] atr) { uint readerLen = 0, atrLen = 0; state = 0; protocol = 0; atr = null; lastError = (WinSCardError)SCardStatus(card, null, ref readerLen, ref state, ref protocol, null, ref atrLen); if (lastError != WinSCardError.SCARD_S_SUCCESS) { return(false); } byte[] buffer = new byte[(int)readerLen]; atr = new byte[atrLen]; lastError = (WinSCardError)SCardStatus(card, buffer, ref readerLen, ref state, ref protocol, atr, ref atrLen); if (lastError != WinSCardError.SCARD_S_SUCCESS) { return(false); } return(true); }
/// <summary> /// カードの切断 /// </summary> /// <param name="card">カードコンテキスト</param> /// <param name="disposition">0を指定</param> /// <returns></returns> public static bool Disconnect(IntPtr card, uint disposition) { lastError = (WinSCardError)SCardDisconnect(card, disposition); Debug.WriteLine("WinSCard.Disconnect Res:" + lastError.ToString()); return(lastError == WinSCardError.SCARD_S_SUCCESS); }
/// <summary> /// リーダーライター接続 /// </summary> /// <param name="rwContext">[OUT]RWコンテキスト</param> /// <returns></returns> public static bool EstablishContext(ref IntPtr rwContext) { lastError = (WinSCardError)SCardEstablishContext(SCARD_SCOPE_USER, IntPtr.Zero, IntPtr.Zero, ref rwContext); return(lastError == WinSCardError.SCARD_S_SUCCESS); }
/// <summary> /// PC/SCエラー /// </summary> /// <param name="apiError">APIエラーコード</param> /// <param name="detail">詳細</param> public PcscException(WinSCardError apiError, byte[] detail) { this.ApiError = apiError; this.Detail = detail; }