public SmartCard Connect(string reader, SmartCardShare shareMode, SmartCardProtocols protocol) { IntPtr card; SmartCardProtocols activeProtocol; int ret = SCardConnect(context, reader, shareMode, protocol, out card, out activeProtocol); if (ret != 0) { throw ToException(ret); } //Console.WriteLine("Protocol: {0}", activeProtocol); return(new SmartCard(card)); }
/// <summary> /// C# Friendly quick wrapper Addtional wrapping can found at WinSmartCard and Win SmardCardContext /// </summary> /// <param name="Card">The Card</param> /// <param name="ReaderName"></param> /// <param name="pcchReaderLen"></param> /// <param name="State"></param> /// <param name="Protocol">The Perferd protocol</param> /// <param name="ATR"></param> /// <param name="ATRLen"></param> /// <returns></returns> internal static ErrorCodes SCardStatus(int Card, byte[] ReaderName, ref int ReaderLength, ref SmartCardStatus Status, ref SmartCardProtocols Protocol, byte[] ATR, ref int ATRLen) { int StatusReturn = 0; int ProtocolReturn = 0; ErrorCodes ErrorCode = (ErrorCodes)SCardStatusImport(Card, ReaderName, ref ReaderLength, ref StatusReturn, ref ProtocolReturn, ATR, ref ATRLen); Status = (SmartCardStatus)StatusReturn; Protocol = (SmartCardProtocols)ProtocolReturn; return(ErrorCode); }
/// <summary> /// C# Friendly quick wrapper Addtional wrapping can found at WinSmartCard and Win SmardCardContext /// </summary> /// <param name="Context">Context</param> /// <param name="ReaderName">The Readers Name</param> /// <param name="ShareMode">The Perferd Sandbox</param> /// <param name="Protocol">The Perferd protocol</param> /// <param name="Card">The Card</param> /// <param name="ActiveProtocol">The Active protocol</param> /// <returns>The Error Code</returns> internal static ErrorCodes SCardConnect(int Context, string ReaderName, SmartCardShareTypes ShareMode, SmartCardProtocols Protocol, ref int Card, ref int ActiveProtocol) { return((ErrorCodes)SCardConnectImport(Context, ReaderName, (int)ShareMode, (int)Protocol, ref Card, ref ActiveProtocol)); }
/// <summary> /// Transmits a buffer as a command for the ADPU command formate mostly used by the ACR122 /// </summary> /// <param name="SendCommand"></param> /// <param name="ReceivedResponse"></param> /// <param name="Protocol"></param> /// <returns></returns> public ErrorCodes Control(byte[] SendCommand, out byte[] ReceivedResponse, out bool HasCard, OperationScopes Scope = OperationScopes.SCARD_SCOPE_SYSTEM, SmartCardProtocols Protocol = SmartCardProtocols.SCARD_PROTOCOL_UNDEFINED) { if (Disposed) { throw new ObjectDisposedException("WinSmartCardContext"); } int TempCard = 0; int AProtocol = 0; uint IOTL = (uint)IOTLOperations.IOCTL_SMARTCARD_DIRECT; // 3225264; ReceivedResponse = new byte[256]; int outBytes = ReceivedResponse.Length; if (Card == null) { if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode, WinSCard.GetScardErrMsg(LastResultCode) + "\nError perceived durring Context Establish"); } LastResultCode = WinSCard.SCardConnect(_Context, ConnectedReaderName, SmartCardShareTypes.SCARD_SHARE_DIRECT, 0, ref TempCard, ref AProtocol); if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode, WinSCard.GetScardErrMsg(LastResultCode) + "\nError perceived durring Connect"); } LastResultCode = WinSCard.SCardControl(TempCard, IOTL, SendCommand, ref ReceivedResponse, ref outBytes); if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode, WinSCard.GetScardErrMsg(LastResultCode) + "\nError perceived durring Control"); } LastResultCode = WinSCard.SCardDisconnect(TempCard, SmartCardDispostion.SCARD_RESET_CARD); if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode, WinSCard.GetScardErrMsg(LastResultCode) + "\nError perceived durring Card Release"); } Array.Resize(ref ReceivedResponse, outBytes); HasCard = false; } else { Card.Control(SendCommand, out ReceivedResponse, Scope, Protocol); HasCard = true; } //3136B0 return(LastResultCode); }
/// <summary> /// Connects to the reader with a card /// Note Winscard requires a card /// </summary> /// <param name="ReaderName">The name of the reader to connect to</param> /// <param name="SmartCardShareTypes">The perfered protocols to use</param> /// <returns></returns> public WinSmartCard CardConnect(SmartCardShareTypes SmartCardShareTypes, SmartCardShareTypes ShareType = SmartCardShareTypes.SCARD_SHARE_SHARED, SmartCardProtocols Protocols = SmartCardProtocols.SCARD_PROTOCOL_Any) { if (Disposed) { throw new ObjectDisposedException("WinSmartCardContext"); } int Card = 0; int Protocol = 0; LastResultCode = WinSCard.SCardConnect(_Context, ConnectedReaderName, ShareType, Protocols, ref Card, ref Protocol); if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode); } this.Card = new WinSmartCard(this, Card); return(this.Card); }
internal SmartCardIORequest(SmartCardProtocols protocol) { this.protocol = protocol; len = (uint)Marshal.SizeOf(typeof(SmartCardIORequest)); }
internal SmartCardIORequest(SmartCardProtocols protocol) { this.protocol = protocol; len = (uint)Marshal.SizeOf(typeof(SmartCardIORequest)); }
private static extern int SCardGetStatus(IntPtr card, IntPtr readerName, ref uint readerLength, out SmartCardState state, out SmartCardProtocols protocol, IntPtr atr, out uint atrLength);
private static extern int SCardConnect(IntPtr context, [MarshalAs(UnmanagedType.LPStr)] string reader, SmartCardShare shareMode, SmartCardProtocols preferredProtocols, out IntPtr card, out SmartCardProtocols activeProtocol);
private static extern int SCardGetStatus(IntPtr card, IntPtr readerName, ref uint readerLength, out SmartCardState state, out SmartCardProtocols protocol, IntPtr atr, out uint atrLength);
private static extern int SCardConnect(IntPtr context, [MarshalAs(UnmanagedType.LPStr)] string reader, SmartCardShare shareMode, SmartCardProtocols preferredProtocols, out IntPtr card, out SmartCardProtocols activeProtocol);
public SmartCard Connect(string reader, SmartCardShare shareMode, SmartCardProtocols protocol) { IntPtr card; SmartCardProtocols activeProtocol; int ret = SCardConnect(context, reader, shareMode, protocol, out card, out activeProtocol); if (ret != 0) throw ToException(ret); //Console.WriteLine("Protocol: {0}", activeProtocol); return new SmartCard(card); }