pcsc-sharp : PCSC Namespace

SCardMonitor Class

Monitors a Smart Card reader and triggers events on status changes.

public class SCardMonitor : IDisposable

Remarks

Creates a new thread and calls the SCardContext.GetStatusChange(IntPtr, SCardReaderState[]) of the given PCSC.SCardContext object.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Members

See Also: Inherited members from object.

Public Constructors

Creates a new SCardMonitor object that is able to listen for certain Smart Card / reader changes.
Creates a new SCardMonitor object that is able to listen for certain Smart Card / reader changes.

Public Properties

[read-only]
Monitoring bool . Indicates if there are readers currently monitored.
[read-only]
ReaderCount int . The number of readers that currently being monitored.
[read-only]
ReaderNames string[]. All readers that are currently being monitored.

Public Methods

Cancel ()
Cancels the monitoring of all readers that are currently being monitored.
Dispose ()
Disposes the object.
Finalize ()
GetCurrentState (int) : SCRState
Returns the current state of a reader that is currently being monitored.
GetCurrentStateValue (int) : IntPtr
Returns the current state of a reader that is currently being monitored.
GetReaderName (int) : string
Returns the reader name of a given index.
Start (string)
Starts to monitor a single Smart Card reader for status changes.
Start (string[])
Starts to monitor a range Smart Card readers for status changes.

Public Events

CardInserted A new card has been inserted.
CardRemoved A card has been removed.
Initialized The monitor object has been initialized.
MonitorException An PC/SC error occurred during monitoring.
StatusChanged A general reader status change.

Member Details

SCardMonitor Constructor

Creates a new SCardMonitor object that is able to listen for certain Smart Card / reader changes.

public SCardMonitor (SCardContext hContext)

Parameters

hContext
A new Application Context to the PC/SC Resource Manager.

Remarks

The monitor object should use its own Application Context to the PC/SC Resource Manager. It will create a (new) backgroud thread that will listen for status changes.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

SCardMonitor Constructor

Creates a new SCardMonitor object that is able to listen for certain Smart Card / reader changes.

public SCardMonitor (SCardContext hContext, SCardScope scope)

Parameters

hContext
A new Application Context to the PC/SC Resource Manager.
scope
Scope of the establishment. This can either be a local or remote connection.

Remarks

The monitor object should use its own Application Context to the PC/SC Resource Manager. It will create a (new) backgroud thread that will listen for status changes.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Cancel Method

Cancels the monitoring of all readers that are currently being monitored.

public void Cancel ()

Remarks

This will end the monitoring. The method calls the SCardContext.Cancel() method of its Application Context to the PC/SC Resource Manager.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

CardInserted Event

A new card has been inserted.

public event CardInsertedEvent CardInserted

Remarks

C# Example
// Create a monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Point the callback function(s) to the pre-defined method MyCardInsertedMethod.
monitor.CardInserted += new CardInsertedEvent(MyCardInsertedMethod);

// Start to monitor the reader
monitor.Start("OMNIKEY CardMan 5x21 00 01");
  

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

CardRemoved Event

A card has been removed.

public event CardRemovedEvent CardRemoved

Remarks

C# Example
// Create a monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Point the callback function(s) to the pre-defined method MyCardRemovedMethod.
monitor.CardRemoved += new CardRemovedEvent(MyCardRemovedMethod);

// Start to monitor the reader
monitor.Start("OMNIKEY CardMan 5x21 00 01");
  

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Dispose Method

Disposes the object.

public void Dispose ()

Remarks

Dispose will call SCardMonitor.Cancel().

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Finalize Method

void Finalize ()

Remarks

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

GetCurrentState Method

Returns the current state of a reader that is currently being monitored.

public SCRState GetCurrentState (int index)

Parameters

index
The number of the desired reader. The index must be between 0 and (SCardMonitor.ReaderCount - 1).

Returns

The current state of reader with index number index.

Remarks

This method will throw an ArgumentOutOfRangeException if the specified index is invalid. You can enumerate all readers currently monitored with the SCardMonitor.ReaderNames property.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

GetCurrentStateValue Method

Returns the current state of a reader that is currently being monitored.

public IntPtr GetCurrentStateValue (int index)

Parameters

index
The number of the desired reader. The index must be between 0 and (SCardMonitor.ReaderCount - 1).

Returns

The current state of reader with index number index.

Remarks

This method will throw an ArgumentOutOfRangeException if the specified index is invalid. You can enumerate all readers currently monitored with the SCardMonitor.ReaderNames property.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

GetReaderName Method

Returns the reader name of a given index.

public string GetReaderName (int index)

Parameters

index
The number of the desired reader. The index must be between 0 and (SCardMonitor.ReaderCount - 1).

Returns

A reader name.

Remarks

This method will throw an ArgumentOutOfRangeException if the specified index is invalid. You can enumerate all readers currently monitored with the SCardMonitor.ReaderNames property.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Initialized Event

The monitor object has been initialized.

public event CardInitializedEvent Initialized

Remarks

This event appears only once for each reader after calling SCardMonitor.Start(string) or SCardMonitor.Start(string[]).

C# Example
// Create a monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Point the callback function(s) to the pre-defined method MyCardInitializedMethod.
monitor.Initialized += new CardInitializedEvent(MyCardInitializedMethod);

// Start to monitor the reader
monitor.Start("OMNIKEY CardMan 5x21 00 01");
  

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

MonitorException Event

An PC/SC error occurred during monitoring.

public event MonitorExceptionEvent MonitorException

Remarks

C# Example
// Create a monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Point the callback function(s) to the pre-defined method MyMonitorExceptionMethod.
monitor.MonitorException += new MonitorExceptionEvent(MyMonitorExceptionMethod);

// Start to monitor the reader
monitor.Start("OMNIKEY CardMan 5x21 00 01");
  

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Monitoring Property

Indicates if there are readers currently monitored.

public bool Monitoring { get; }

Value

Value Description
true Monitoring process ongoing.
false No monitoring.

Remarks

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

ReaderCount Property

The number of readers that currently being monitored.

public int ReaderCount { get; }

Value

Return 0 if no reader is being monitored.

Remarks

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

ReaderNames Property

All readers that are currently being monitored.

public string[] ReaderNames { get; }

Value

A string array of reader names. null if no readers is being monitored.

Remarks

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Start Method

Starts to monitor a single Smart Card reader for status changes.

public void Start (string readername)

Parameters

readername
The Smart Card reader that shall be monitored.

Remarks

C# Example
// Create a new monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Start to monitor a single reader.
monitor.Start("OMNIKEY CardMan 5x21 00 00");

  

Do not forget to register for at least one event:
EventDescription
SCardMonitor.CardInsertedA new card has been inserted.
SCardMonitor.CardRemovedA card has been removed.
SCardMonitor.InitializedInitial status.
SCardMonitor.StatusChangedA general status change.
SCardMonitor.MonitorExceptionAn error occurred.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

Start Method

Starts to monitor a range Smart Card readers for status changes.

public void Start (string[] readernames)

Parameters

readernames
A string array of reader names that shall be monitored.

Remarks

C# Example
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

// Retrieve the names of all installed readers.
string[] readernames = ctx.GetReaders();

ctx.Release();

// Create a new monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

foreach (string reader in readernames)
	Console.WriteLine("Start monitoring for reader " + reader + ".");
        
// Start monitoring multiple readers.
monitor.Start(readernames);

Do not forget to register for at least one event:
EventDescription
SCardMonitor.CardInsertedA new card has been inserted.
SCardMonitor.CardRemovedA card has been removed.
SCardMonitor.InitializedInitial status.
SCardMonitor.StatusChangedA general status change.
SCardMonitor.MonitorExceptionAn error occurred.

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1

StatusChanged Event

A general reader status change.

public event StatusChangeEvent StatusChanged

Remarks

C# Example
// Create a monitor object with its own PC/SC context.
SCardMonitor monitor = new SCardMonitor(
	new SCardContext(),
	SCardScope.System);

// Point the callback function(s) to the pre-defined method MyStatusChangedMethod.
monitor.StatusChanged += new StatusChangeEvent(MyStatusChangedMethod);

// Start to monitor the reader
monitor.Start("OMNIKEY CardMan 5x21 00 01");
  

Requirements

Namespace: PCSC
Assembly: pcsc-sharp (in pcsc-sharp.dll)
Assembly Versions: 1.0.0.0, 2.0.0.0, 2.0.0.1