/// <summary> /// Establishes a context for winscard operations. /// </summary> /// <param name="context">An IntPtr that will be set to the context.</param> /// <param name="result">A SCardResponse that will be set to the result of the operation.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool EstablishContext(out IntPtr context, out SCardResponse result) { uint resultCode; bool returnValue = false; resultCode = SCardEstablishContext(SCardContextScope.SCOPE_SYSTEM, IntPtr.Zero, IntPtr.Zero, out context); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } return(returnValue); }
/// <summary> /// Disconnects the specified card. /// </summary> /// <param name="cardHandle">An IntPtr that is the handle to the card.</param> /// <param name="result">A SCardResponse that will be set to the result of the operation.</param> /// <param name="cardDisposition">A SCardDisposition that indicates the action to take in the connected reader once disconnected.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool DisconnectCard(IntPtr cardHandle, out SCardResponse result, SCardDisposition cardDisposition = SCardDisposition.LeaveCard) { uint resultCode; bool returnValue = false; resultCode = SCardDisconnect(cardHandle, cardDisposition); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } return(returnValue); }
/// <summary> /// Releases a context from winscard operations. /// </summary> /// <param name="context">An IntPtr that is the context to release.</param> /// <param name="result">A SCardResponse that will be set to the result of the operation.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool ReleaseContext(IntPtr context, out SCardResponse result) { bool returnValue = false; // Defaults result = SCardResponse.SCARD_E_INVALID_HANDLE; // Make sure there is a valid context to release if (context != IntPtr.Zero) { uint resultCode; resultCode = SCardReleaseContext(context); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } } return(returnValue); }
internal static bool TransmitToCard(IntPtr cardHandle, SCardProtocol protocol, byte[] data, out byte[] response, out SCardResponse result) { bool returnValue = false; // Defaults response = null; result = SCardResponse.SCARD_E_INVALID_HANDLE; if (cardHandle != IntPtr.Zero) { SCARD_IO_REQUEST receiveProtocol; uint resultCode; SCARD_IO_REQUEST sendProtocol; // Setup sendProtocol = GetNewIoRequest(protocol); receiveProtocol = GetNewIoRequest(protocol); resultCode = SCardTransmit(cardHandle, ref sendProtocol, data, (uint)data.Length, ref receiveProtocol, null, out uint receivedSize); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { response = new byte[receivedSize]; resultCode = SCardTransmit(cardHandle, ref sendProtocol, data, (uint)data.Length, ref receiveProtocol, response, out receivedSize); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } } } return(returnValue); }
/// <summary> /// Lists readers in the specified group using the specified context. /// </summary> /// <param name="context">An IntPtr that is the context to use.</param> /// <param name="group">A SCardReaderGroup that is the reader group to use.</param> /// <param name="readers">An array of string that will set to the list of readers.</param> /// <param name="result">A SCardResponse that will bes et to the result of the operation.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool ListReaders(IntPtr context, SCardReaderGroup group, out string[] readers, out SCardResponse result) { uint readerSize = 0; uint resultCode; bool returnValue = false; // Defaults readers = null; resultCode = SCardListReaders(context, GetGroupName(group), null, ref readerSize); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { string readerBuffer; readerBuffer = "".PadLeft((int)readerSize); resultCode = SCardListReaders(context, GetGroupName(group), readerBuffer, ref readerSize); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { readers = readerBuffer.Split('\0').Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Trim()).ToArray(); returnValue = true; } } return(returnValue); }
/// <summary> /// Gets the status of the specified reader states, blocking the current thread until the specified timeout. /// </summary> /// <param name="context">An IntPtr that is the context to use.</param> /// <param name="readerStates">An array of SCARD_READERSTATE that are the readers to watch.</param> /// <param name="timeout">An uint indicating the timeout period in milliseconds.</param> /// <param name="result">A SCardResponse that will bes et to the result of the operation.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool GetStatusChange(IntPtr context, SCARD_READERSTATE[] readerStates, uint timeout, out SCardResponse result) { uint resultCode; bool returnValue = false; resultCode = SCardGetStatusChange(context, timeout, readerStates, (uint)readerStates.Length); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } return(returnValue); }
/// <summary> /// Connects to a card on the specified reader using the specified parameters. /// </summary> /// <param name="context">An IntPtr that is the context to use.</param> /// <param name="readerName">A string containing the name of the reader to use.</param> /// <param name="cardHandle">An IntPtr that will be set to the handle of the card.</param> /// <param name="actualProtocol">A SCardProtocol that will be set to the established protocol.</param> /// <param name="result">A SCardResponse that will be set to the result of the operation.</param> /// <param name="shareMode">A SCardShareMode that indicates other applications can connect to the card.</param> /// <param name="protocol">A SCardProtocol indicating the acceptable protocols to use for the connection.</param> /// <returns>A bool that indicates whether the operation was a success.</returns> internal static bool ConnectCard(IntPtr context, string readerName, out IntPtr cardHandle, out SCardProtocol actualProtocol, out SCardResponse result, SCardShareMode shareMode = SCardShareMode.Shared, SCardProtocol protocol = SCardProtocol.T0) { uint resultCode; bool returnValue = false; resultCode = SCardConnect(context, readerName, shareMode, protocol, out cardHandle, out actualProtocol); result = (SCardResponse)resultCode; if (result == SCardResponse.SCARD_S_SUCCESS) { returnValue = true; } return(returnValue); }