public void Connect(SCardScope scope) { // If we are already connected, ignore if (myContext != IntPtr.Zero) { return; } // Establish a resource manager context PCSCResult result = NativeMethods.SCardEstablishContext(scope, IntPtr.Zero, IntPtr.Zero, out myContext); if (result != PCSCResult.None) { throw new PCSCException("PCSCManager.Connect(): Unable to establish resource manager context"); } // Create our reader list myReaders = new Dictionary <string, PCSCReader>(); string[] readers = ListReaders(); // Check for our initial list of readers readers foreach (string r in readers) { lock (myReaders) { myReaders.Add(r, new PCSCReader(r)); } SpawnOnReaderInsertedEvent(r); } }
public void Reconnect(SCardDisposition initialisation) { PCSCResult result = NativeMethods.SCardReconnect(myHandle, DefaultShareMode, DefaultRequestedProtocols, initialisation, ref myCommsProtocol); if (result != PCSCResult.None) { // Disconnect from the card Disconnect(); throw PCSCException.ThrowByResult(result); } }
/// <summary> /// A simple check to see if the PC/SC service is able to perform smartcard operations /// </summary> /// <returns></returns> public bool IsPCSCAvailable() { IntPtr context = IntPtr.Zero; PCSCResult result = NativeMethods.SCardEstablishContext(SCardScope.System, IntPtr.Zero, IntPtr.Zero, out context); if (result == PCSCResult.None) { NativeMethods.SCardReleaseContext(context); } return(result == PCSCResult.None); }
public string[] ListReaders(string readerGroup) { PCSCResult result = PCSCResult.None; char[] delimiter = new char[] { '\0' }; string readers = ""; if (String.IsNullOrEmpty(readerGroup)) { uint len = 0; // Get the buffer length NativeMethods.SCardListReaders(myContext, null, null, ref len); // Get the reader string byte[] buffer = new byte[len]; result = NativeMethods.SCardListReaders(myContext, null, buffer, ref len); readers = new ASCIIEncoding().GetString(buffer, 0, buffer.Length); } else { uint len = 0; byte[] groups = new ASCIIEncoding().GetBytes(readerGroup); // Get the buffer length NativeMethods.SCardListReaders(myContext, groups, null, ref len); // Get the reader string byte[] buffer = new byte[len]; result = NativeMethods.SCardListReaders(myContext, groups, buffer, ref len); readers = new ASCIIEncoding().GetString(buffer, 0, buffer.Length); } if (result != PCSCResult.None) { if (result == PCSCResult.NoReadersAvailable) { return(new string[0]); } else { throw new PCSCException("ListReaders(): Error listing readers."); } } // Trim off the trailing \0 chars readers = readers.Substring(0, readers.Length - 2); return(readers.Split(delimiter)); }
protected override bool ReleaseHandle() { if (!IsInvalid) { PCSCResult result = NativeMethods.SCardReleaseContext(this.handle); this.handle = IntPtr.Zero; return(result == PCSCResult.None); } else { return(true); } }
public string[] ListReaderGroups() { PCSCResult result = 0; char[] delimiter = new char[] { '\0' }; uint length = 0; string groups = ""; result = NativeMethods.SCardListReaderGroups(myContext, ref groups, ref length); if (result != PCSCResult.None) { throw new PCSCException("ListReaderGroups(): Error listing groups."); } return(groups.Split(delimiter)); }
public override RApdu Transceive(CApdu command) { IntPtr requestPci; switch (myCard.CommsProtocol) { case SCardProtocol.T0: requestPci = PCSCApi.SCardPciT0; break; case SCardProtocol.T1: requestPci = PCSCApi.SCardPciT1; break; default: throw PCSCException.ThrowUnsupportedProtocol(); } int responseLength = 1024; byte[] responseBuffer = new byte[responseLength]; byte[] cmdBuffer = command.ToArray(); PCSCResult result = NativeMethods.SCardTransmit(myCard.Handle, requestPci, cmdBuffer, cmdBuffer.Length, IntPtr.Zero, responseBuffer, ref responseLength); if (PCSCResult.None == result) { byte[] response = new byte[responseLength]; Array.Copy(responseBuffer, response, responseLength); #if DEBUG RaiseOnTranscieve("CAPDU> " + Converters.ByteArrayToString(cmdBuffer)); RaiseOnTranscieve("RAPDU> " + Converters.ByteArrayToString(response)); #endif return(RApdu.Parse(response)); } else { #if DEBUG RaiseOnTranscieve("CAPDU> " + Converters.ByteArrayToString(cmdBuffer)); RaiseOnTranscieve("RAPDU> Error condition (HRESULT = " + EnumHelper.GetDescription(result) + ")"); #endif throw new PCSCException("Communications error."); } }
internal byte[] GetAttribute(uint attribute) { PCSCResult result = 0; IntPtr len = IntPtr.Zero; // Get the length required result = NativeMethods.SCardGetAttrib(myCard.Handle, attribute, null, ref len); if (result != PCSCResult.None) { throw PCSCException.ThrowByResult(result); } // Get the value byte[] buffer = new byte[len.ToInt32()]; result = NativeMethods.SCardGetAttrib(myCard.Handle, attribute, buffer, ref len); if (result != PCSCResult.None) { throw PCSCException.ThrowByResult(result); } return(buffer); }
public static PCSCException ThrowByResult(PCSCResult result) { return(new PCSCException(EnumHelper.GetDescription(result))); }