/// <summary> /// The EstablishContext function establishes the smart card resourete manager context. /// </summary> /// <param name="dwScope"> /// Scope of the resource manager context. /// </param> public void EstablishContext(SCARD_SCOPE dwScope) { int ret = WinSCardAPIWrapper.SCardEstablishContext((uint)dwScope, IntPtr.Zero, IntPtr.Zero, out phContext); if (ret == 0) { Trace.WriteLineIf(scardTrace, string.Format(" SCard.EstablishContext({0})", (SCARD_SCOPE)dwScope)); } else { throw new WinSCardException(scardTrace, "SCard.EstablishContext", ret); } }
/// <summary> /// Establishes the smart card resourete manager context and selectes the specified reader. /// </summary> /// <param name="szReader"> /// The name of the reader that contains the target card. /// </param> /// <param name="dwScope">Scope of the resource manager context.</param> public void Connect(string szReader, SCARD_SCOPE dwScope) { try { this.SCard.EstablishContext(dwScope); if (!string.IsNullOrEmpty(szReader)) { this.readerName = szReader; return; } this.SCard.ListReaders(); if (this.SCard.ReaderNames.Length == 1) { this.readerName = this.SCard.ReaderNames[0]; return; } Console.WriteLine("------------------------------------------------------"); Console.WriteLine("Available PC/SC Readers:"); Console.WriteLine("------------------------------------------------------"); for (int i = 0; i < this.SCard.ReaderNames.Length; i++) { Console.WriteLine(string.Format("Reader {0:#0}: {1}", i, this.SCard.ReaderNames[i])); } Console.WriteLine(""); Console.Write("Please select a reader (0..n): "); this.readerName = this.SCard.ReaderNames[int.Parse(Console.ReadLine())]; } catch (WinSCardException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public static extern HRESULT SCardEstablishContext(SCARD_SCOPE scope, IntPtr reserved1, IntPtr reserved2, [Out] out IntPtr hContext);
/// <summary> /// Establishes the smart card resourete manager context and provides the list of readers. /// </summary> /// <param name="dwScope">Scope of the resource manager context.</param> public void Connect(SCARD_SCOPE dwScope) { Connect(null, dwScope); }
// The EstablishContext function establishes the smart card resourete manager context. // Scope of the resource manager context. //you can also change the Context such that it operates outside of the local context public void EstablishContext(SCARD_SCOPE dwScope) { //returns zero if successful according to int ret = WinSCardAPIWrapper.SCardEstablishContext( (uint)dwScope, IntPtr.Zero, IntPtr.Zero, out phContext ); if(ret == 0) { Trace.WriteLineIf(scardTrace, string.Format(" SCard.EstablishContext({0})", (SCARD_SCOPE)dwScope)); } else { throw new WinSCardException( scardTrace, "SCard.EstablishContext", ret ); } }