/// <summary> /// Creates a connect at the use scope requested /// </summary> /// <param name="Scope">the scope to use (often System)</param> public WinSmartCardContext(OperationScopes Scope, string ConnectedReaderName) { LastResultCode = WinSCard.SCardEstablishContext(Scope, ref _Context); if (LastResultCode != ErrorCodes.SCARD_S_SUCCESS) { throw new WinSCardException(LastResultCode); } this.ConnectedReaderName = ConnectedReaderName; }
//(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen); /// <summary> /// C# Friendly quick wrapper Addtional wrapping can found at WinSmartCard and Win SmardCardContext /// </summary> /// <param name="Scope">Scope</param> /// <param name="Context">Context</param> /// <returns>The Error Code</returns> internal static ErrorCodes SCardEstablishContext(OperationScopes Scope, ref int Context) { return((ErrorCodes)SCardEstablishContextImport((int)Scope, 0, 0, ref Context)); }
/// <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); }