pcsc-sharp : PCSC Namespace

ISCardReader Interface

Common functions that are needed to operate on Smart Card readers.

public interface ISCardReader

Remarks

See PCSC.SCardReader.

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

Public Properties

[read-only]
ActiveProtocol SCardProtocol . The currently used protocol to communicate with the card.
[read-only]
CardHandle IntPtr . A pointer (Card Handle) that can be used for C API calls.
[read-only]
CurrentContext SCardContext . The Smart Card context that will be used for this connection.
[read-only]
CurrentShareMode SCardShareMode . The current mode of connection type: exclusive or shared.
[read-only]
ReaderName string . The connected reader's friendly name.

Public Methods

BeginTransaction () : SCardError
Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.
Connect (string, SCardShareMode, SCardProtocol) : SCardError
Establishes a connection to the Smart Card reader.
Control (IntPtr, byte[], ref byte[]) : SCardError
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.
Disconnect (SCardReaderDisposition) : SCardError
Terminates a connection made through ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
EndTransaction (SCardReaderDisposition) : SCardError
Ends a previously begun transaction.
GetAttrib (SCardAttr, out byte[]) : SCardError
Gets an attribute from the IFD Handler (reader driver).
GetAttrib (IntPtr, out byte[]) : SCardError
Gets an attribute from the IFD Handler (reader driver).
GetAttrib (SCardAttr, byte[], out int) : SCardError
Gets an attribute from the IFD Handler (reader driver).
GetAttrib (IntPtr, byte[], out int) : SCardError
Gets an attribute from the IFD Handler (reader driver).
Reconnect (SCardShareMode, SCardProtocol, SCardReaderDisposition) : SCardError
Reestablishes a connection to a reader that was previously connected to using ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
SetAttrib (SCardAttr, byte[]) : SCardError
Set an attribute of the IFD Handler.
SetAttrib (IntPtr, byte[]) : SCardError
Set an attribute of the IFD Handler.
SetAttrib (SCardAttr, byte[], int) : SCardError
Set an attribute of the IFD Handler.
SetAttrib (IntPtr, byte[], int) : SCardError
Set an attribute of the IFD Handler.
Status (out string[], out SCardState, out SCardProtocol, out byte[]) : SCardError
Returns the current status of the reader and the connected card.
Transmit (byte[], ref byte[]) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (byte[], byte[], ref int) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (IntPtr, byte[], ref byte[]) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (SCardPCI, byte[], SCardPCI, ref byte[]) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (byte[], int, byte[], ref int) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (IntPtr, byte[], SCardPCI, ref byte[]) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).
Transmit (IntPtr, byte[], int, SCardPCI, byte[], ref int) : SCardError
Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

Member Details

ActiveProtocol Property

The currently used protocol to communicate with the card.

public SCardProtocol ActiveProtocol { get; }

Value

SCardProtocol.Unset if not connected.

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

BeginTransaction Method

Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.

public SCardError BeginTransaction ()

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.SharingViolation Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

You might want to use this when you are selecting a few files and then writing a large file so you can make sure that another application will not change the current file. If another application has a lock on this reader or this application is in SCardShareMode.Exclusive there will be no action taken.

This method calls the API function SCardBeginTransaction().

Requirements

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

CardHandle Property

A pointer (Card Handle) that can be used for C API calls.

public IntPtr CardHandle { get; }

Value

0 if not connected.

Remarks

This is the card handle that is returned when calling the C API function SCardConnect().

Requirements

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

Connect Method

Establishes a connection to the Smart Card reader.

public SCardError Connect (string name, SCardShareMode mode, SCardProtocol prefProto)

Parameters

name
Reader name to connect to.
mode
Mode of connection type: exclusive or shared.
prefProto
Desired protocol use.

Returns

An error code / return value:
Error codeDescription
SCardError.SuccessSuccessful (SCARD_S_SUCCESS)
SCardError.InvalidHandleInvalid context handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameterprefProto is invalid or null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValueInvalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)
SCardError.NoServiceThe server is not runing (SCARD_E_NO_SERVICE)
SCardError.NoSmartCardNo smart card present (SCARD_E_NO_SMARTCARD)
SCardError.NotReadyCould not allocate the desired port (SCARD_E_NOT_READY)
SCardError.ProtocolMismatchRequested protocol is unknown (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailableCould not power up the reader or card (SCARD_E_READER_UNAVAILABLE)
SCardError.SharingViolationSomeone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCardError.UnknownReaderThe reader name is null (SCARD_E_UNKNOWN_READER)
SCardError.UnsupportedFeatureProtocol not supported (SCARD_E_UNSUPPORTED_FEATURE)
SCardError.CommunicationErrorAn internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.InternalErrorAn internal consistency check failed (SCARD_F_INTERNAL_ERROR)
SCardError.UnpoweredCardCard is not powered (SCARD_W_UNPOWERED_CARD)
SCardError.UnresponsiveCardCard is mute (SCARD_W_UNRESPONSIVE_CARD)

Remarks

prefProto is a bit mask of acceptable protocols for the connection. You can use (SCardProtocol.T0 | SCardProtocol.T1) if you do not have a preferred protocol. The protocol used with this connection will be stored in ISCardReader.ActiveProtocol.

This method calls the API function SCardConnect().

C# Example
// Establish PC/SC context.
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

// Create a Smart Card reader object and connect to it.
ISCardReader reader = new SCardReader(ctx);
SCardError serr = reader.Connect("OMNIKEY CardMan 5x21 00 00",
	SCardShareMode.Shared,
	SCardProtocol.Any);
  

Requirements

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

Control Method

Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.

public SCardError Control (IntPtr controlCode, byte[] sendBuffer, ref byte[] recvBuffer)

Parameters

controlCode
Control code for the operation.
sendBuffer
Command to send to the reader.
recvBuffer
Response from the reader.

Returns

Return code Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer is null and the IFDHandler is version 2.0 (without controlCode) (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid value was presented (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed(SCARD_E_READER_UNAVAILABLE)
SCardError.UnsupportedFeature Driver does not support (SCARD_E_UNSUPPORTED_FEATURE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.RemovedCard The card has been removed from the reader(SCARD_W_REMOVED_CARD)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)

Remarks

This method is useful for creating client side reader drivers for functions like PIN pads, biometrics, or other extensions to the normal smart card reader that are not normally handled by PC/SC.

This method calls the API function SCardControl().

Note:

The API of this function changed. In pcsc-lite 1.2.0 and before the API was not Windows(R) PC/SC compatible. This has been corrected.

Requirements

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

CurrentContext Property

The Smart Card context that will be used for this connection.

public SCardContext CurrentContext { get; }

Value

null if the reader is not connected.

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

CurrentShareMode Property

The current mode of connection type: exclusive or shared.

public SCardShareMode CurrentShareMode { get; }

Value

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

Disconnect Method

Terminates a connection made through ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Disconnect (SCardReaderDisposition discntExec)

Parameters

discntExec
Reader function to execute.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidValue Invalid discntExec (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NoSmartcard No smart card present (SCARD_E_NO_SMARTCARD)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

This method calls the API function SCardDisconnect().

C# Example
// Establish PC/SC context.
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

// Create a Smart Card reader object and connect to it.
ISCardReader reader = new SCardReader(ctx);
SCardError serr = reader.Connect("OMNIKEY",
	SCardShareMode.Shared,
	SCardProtocol.Any);

// Disconnect the reader and reset the SmartCard.
reader.Disconnect(SCardReaderDisposition.Reset);
  

Requirements

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

EndTransaction Method

Ends a previously begun transaction.

public SCardError EndTransaction (SCardReaderDisposition disposition)

Parameters

disposition
Action to be taken on the reader.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidValue Invalid value for disposition (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.SharingViolation Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The calling application must be the owner of the previously begun transaction or an error will occur.

This method calls the API function SCardEndTransaction().

Note:

The disposition action is not currently used in PC/SC Lite on UNIX/Linux machines.

Requirements

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

GetAttrib Method

Gets an attribute from the IFD Handler (reader driver).

public SCardError GetAttrib (SCardAttr AttrId, out byte[] pbAttr)

Parameters

AttrId
Identifier for the attribute to get.
pbAttr
A buffer that receives the attribute.

Returns

Return code Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big - indicates an error in the PC/SC class library. (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InsufficientBuffer Reader buffer pbAttr not large enough - indicates an error in the PC/SC class library. (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoMemory Memory allocation failed (SCARD_E_NO_MEMORY)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

This method calls the API function SCardGetAttrib().

C# Example
// Helper function that translates a byte array into an hex-encoded ATR string.
static string StringAtr(byte[] atr)
{
	if (atr == null)
	return null;

	StringBuilder sb = new StringBuilder();
	foreach (byte b in atr)
		sb.AppendFormat("{0:X2}", b);

	return sb.ToString();
}
  
C# Example
// Create a new PC/SC context.
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

string[] readernames = ctx.GetReaders();
if (readernames == null || readernames.Length == 0)
	throw new Exception("You need at least one reader in order to run this example.");

// Receive the ATR of each reader by using the GetAttrib function
foreach (string name in readernames)
{
	SCardReader reader = new SCardReader(ctx);

	Console.Write("Trying to connect to reader.. " + name);
                
	// Connect to the reader, error if no card present.
	SCardError rc = reader.Connect(
		name,
		SCardShareMode.Exclusive,
		SCardProtocol.Any);

	if (rc == SCardError.Success)
	{
		// Reader is now connected.
		Console.WriteLine(" done.");

		// receive ATR string attribute
		byte[] atr;
		rc = reader.GetAttrib(SCardAttr.ATRString, out atr);

		if (rc != SCardError.Success)
		{
			// ATR not supported?
			Console.WriteLine("Error by trying to receive the ATR. "
				+ SCardHelper.StringifyError(rc) + "\n");
		} else {
			Console.WriteLine("ATR: " + StringAtr(atr) + "\n");
		}

		// Disconnect
		reader.Disconnect(SCardReaderDisposition.Leave);
	} else  {
		// Probably no SmartCard present.
		Console.WriteLine(" failed. " + SCardHelper.StringifyError(rc) + "\n");
	}
}
ctx.Release();
  

Requirements

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

GetAttrib Method

Gets an attribute from the IFD Handler (reader driver).

public SCardError GetAttrib (IntPtr AttrId, out byte[] pbAttr)

Parameters

AttrId
Identifier for the attribute to get.
pbAttr
A buffer that receives the attribute.

Returns

Return code Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big - indicates an error in the PC/SC class library. (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InsufficientBuffer Reader buffer pbAttr not large enough - indicates an error in the PC/SC class library. (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoMemory Memory allocation failed (SCARD_E_NO_MEMORY)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

This method calls the API function SCardGetAttrib().

Requirements

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

GetAttrib Method

Gets an attribute from the IFD Handler (reader driver).

public SCardError GetAttrib (SCardAttr AttrId, byte[] pbAttr, out int AttrLen)

Parameters

AttrId
Identifier for the attribute to get.
pbAttr
A buffer that receives the attribute.
AttrLen
Size of the result contained in pbAttr (in bytes).

Returns

Return code Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InsufficientBuffer Reader buffer pbAttr not large enough (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoMemory Memory allocation failed (SCARD_E_NO_MEMORY)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The method will return SCardError.InsufficientBuffer if pbAttr is null or if the buffer size is not sufficient. AttrLen contains the required amount of bytes (size).

If the method returned with SCardError.Success then AttrLen contains the exact size of the result in pbAttr.

For an example please see ISCardReader.GetAttrib(SCardAttr, Byte[]@).

This method calls the API function SCardGetAttrib().

Requirements

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

GetAttrib Method

Gets an attribute from the IFD Handler (reader driver).

public SCardError GetAttrib (IntPtr dwAttrId, byte[] pbAttr, out int AttrLen)

Parameters

dwAttrId
Identifier for the attribute to get.
pbAttr
A buffer that receives the attribute.
AttrLen
Size of the result contained in pbAttr (in bytes).

Returns

Return code Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InsufficientBuffer Reader buffer pbAttr not large enough (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoMemory Memory allocation failed (SCARD_E_NO_MEMORY)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The method will return SCardError.InsufficientBuffer if pbAttr is null or if the buffer size is not sufficient. AttrLen contains the required amount of bytes (size).

If the method returned with SCardError.Success then AttrLen contains the exact size of the result in pbAttr.

This method calls the API function SCardGetAttrib().

Requirements

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

ReaderName Property

The connected reader's friendly name.

public string ReaderName { get; }

Value

A human readable string of the reader name or null if the reader object is currently not connected.

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

Reconnect Method

Reestablishes a connection to a reader that was previously connected to using ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Reconnect (SCardShareMode mode, SCardProtocol prefProto, SCardReaderDisposition initExec)

Parameters

mode
Mode of connection type: exclusive/shared.
ValueDescription
SCardShareMode.SharedThis application will allow others to share the reader. (SCARD_SHARE_SHARED)
SCardShareMode.ExclusiveThis application will NOT allow others to share the reader. (SCARD_SHARE_EXCLUSIVE)
prefProto
Desired protocol use.
initExec
Desired action taken on the card/reader.

Returns

An error code / return value:
Error codeDescription
SCardError.SuccessSuccessful (SCARD_S_SUCCESS)
SCardError.InvalidHandleInvalid context handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameterprefProto is invalid or null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValueInvalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)
SCardError.NoServiceThe server is not runing (SCARD_E_NO_SERVICE)
SCardError.NoSmartCardNo smart card present (SCARD_E_NO_SMARTCARD)
SCardError.NotReadyCould not allocate the desired port (SCARD_E_NOT_READY)
SCardError.ProtocolMismatchRequested protocol is unknown (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailableCould not power up the reader or card (SCARD_E_READER_UNAVAILABLE)
SCardError.SharingViolationSomeone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCardError.UnsupportedFeatureProtocol not supported (SCARD_E_UNSUPPORTED_FEATURE)
SCardError.CommunicationErrorAn internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.InternalErrorAn internal consistency check failed (SCARD_F_INTERNAL_ERROR)
SCardError.RemovedCardThe smart card has been removed (SCARD_W_REMOVED_CARD)
SCardError.UnresponsiveCardCard is mute (SCARD_W_UNRESPONSIVE_CARD)

Remarks

prefProto is a bit mask of acceptable protocols for the connection. You can use (SCardProtocol.T0 | SCardProtocol.T1) if you do not have a preferred protocol. The protocol used with this connection will be stored in ISCardReader.ActiveProtocol.

This method calls the API function SCardReconnect().

Requirements

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

SetAttrib Method

Set an attribute of the IFD Handler.

public SCardError SetAttrib (SCardAttr attr, byte[] pbAttr)

Parameters

attr
Identifier for the attribute to set.
pbAttr
Buffer that contains the new value of the attribute.

Returns

Column Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The list of attributes you can set is dependent on the IFD Handler you are using.

This method calls the API function SCardSetAttrib().

Requirements

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

SetAttrib Method

Set an attribute of the IFD Handler.

public SCardError SetAttrib (IntPtr attr, byte[] pbAttr)

Parameters

attr
Identifier for the attribute to set.
pbAttr
Buffer that contains the new value of the attribute.

Returns

Column Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The list of attributes you can set is dependent on the IFD Handler you are using.

This method calls the API function SCardSetAttrib().

Requirements

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

SetAttrib Method

Set an attribute of the IFD Handler.

public SCardError SetAttrib (SCardAttr attr, byte[] pbAttr, int AttrBufSize)

Parameters

attr
Identifier for the attribute to set.
pbAttr
Buffer that contains the new value of the attribute.
AttrBufSize
Length of the pbAttr buffer in bytes.

Returns

Column Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The list of attributes you can set is dependent on the IFD Handler you are using.

This method calls the API function SCardSetAttrib().

Requirements

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

SetAttrib Method

Set an attribute of the IFD Handler.

public SCardError SetAttrib (IntPtr attr, byte[] pbAttr, int AttrBufSize)

Parameters

attr
Identifier for the attribute to set.
pbAttr
Buffer that contains the new value of the attribute.
AttrBufSize
Length of the pbAttr buffer in bytes.

Returns

Column Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer pbAttr is too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter A parameter is null and should not (SCARD_E_INVALID_PARAMETER)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted Data exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)

Remarks

The list of attributes you can set is dependent on the IFD Handler you are using.

This method calls the API function SCardSetAttrib().

Requirements

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

Status Method

Returns the current status of the reader and the connected card.

public SCardError Status (out string[] ReaderName, out SCardState State, out SCardProtocol Protocol, out byte[] Atr)

Parameters

ReaderName
The connected readers's friendly name.
State
The current state.
Protocol
The card's currently used protocol.
Atr
The card's ATR.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer The reader object did not allocate enough memory for ReaderName or for Atr (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle The reader object got invalid. Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter The reader object passed a size of null for ReaderName or Atr (SCARD_E_INVALID_PARAMETER)
SCardError.NoMemory Memory allocation failed (SCARD_E_NO_MEMORY)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.InternalError An internal consistency check failed (SCARD_F_INTERNAL_ERROR)
SCardError.RemovedCard The smart card has been removed (SCARD_W_REMOVED_CARD)
SCardError.ResetCard The smart card has been reset (SCARD_W_RESET_CARD)

Remarks

The connected readers's friendly name will be stored in ReaderName. The card's ATR will be stored in Atr. The current state, and protocol will be stored in State and Protocol respectively.

This method calls the API function SCardStatus().

C# Example
// Create a new PC/SC context.
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

// retrieve all reader names
string[] readernames = ctx.GetReaders();

if (readernames != null)
{
	// get the card status of each reader that is currently connected
	foreach (string readername in readernames)
	{
		SCardReader reader = new SCardReader(ctx);
		Console.Write("Trying to connect to reader " + readername + "..");

		SCardError serr = reader.Connect(readername,
			SCardShareMode.Shared,
			SCardProtocol.Any);

		if (serr == SCardError.Success)
		{
			// SmartCard inserted, reader is now connected.
			Console.WriteLine(" done.");

			string[] tmpreadernames;
			SCardProtocol proto;
			SCardState state;
			byte[] atr;

			serr = reader.Status(
				out tmpreadernames, // contains the reader name(s)
				out state,          // contains the current state (flags)
				out proto,          // contains the currently used communication protocol
				out atr);           // contains the card ATR

			if (serr == SCardError.Success)
			{
				Console.WriteLine("Connected with protocol " +
					proto + " in state " + state);
				if (atr != null && atr.Length != 0)
				{
					Console.Write("Card ATR: ");
					foreach (byte b in atr)
						Console.Write("{0:X2}", b);
					Console.WriteLine();
				}
				Console.WriteLine();
			} else {
				Console.WriteLine("Unable to retrieve card status.\nError message: "
					+ SCardHelper.StringifyError(serr)
					+ ".\n");
			}

			reader.Disconnect(SCardReaderDisposition.Reset);
		} else {
			/* SmardCard not inserted or reader is reserved exclusively by
				another application. */
			Console.WriteLine(" failed.\nError message: "
				+ SCardHelper.StringifyError(serr) + ".\n");
		}
	}
}
  

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (byte[] sendBuffer, ref byte[] recvBuffer)

Parameters

sendBuffer
APDU to send to the card.
recvBuffer
Response from the card.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. The buffer recvBuffer must be initialized and will be resized, if the buffer was too big.

This method calls the API function SCardTransmit().

Note:

This method will only work if the reader has been connected with one of the following protocols:
ProtocolDescription
SCardProtocol.T0T=0 active protocol.
SCardProtocol.T1T=1 active protocol.
SCardProtocol.RawRaw active protocol.

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (byte[] sendBuffer, byte[] recvBuffer, ref int recvBufferLength)

Parameters

sendBuffer
APDU to send to the card.
recvBuffer
Response from the card.
recvBufferLength
The buffer size of recvBuffer in bytes.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. The buffer recvBuffer must be initialized. The size of the returned data will be stored in recvBufferLength. This method will return with error code SCardError.InsufficientBuffer if the buffer size of recvBuffer is too small for the result. If the parameter recvBufferLength is invalid, the method will throw an ArgumentOutOfRangeException.

This method calls the API function SCardTransmit().

Note:

This method will only work if the reader has been connected with one of the following protocols:
ProtocolDescription
SCardProtocol.T0T=0 active protocol.
SCardProtocol.T1T=1 active protocol.
SCardProtocol.RawRaw active protocol.

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (IntPtr pioSendPci, byte[] sendBuffer, ref byte[] recvBuffer)

Parameters

pioSendPci
A pointer to a pre-defined Structure of Protocol Control Information. You can use one of the following:
Protocol Control InformationDescription
SCardPCI.T0Pre-defined T=0 PCI structure. (SCARD_PCI_T0)
SCardPCI.T1Pre-defined T=1 PCI structure. (SCARD_PCI_T1)
SCardPCI.RawPre-defined RAW PCI structure. (SCARD_PCI_RAW)
sendBuffer
APDU to send to the card.
recvBuffer
Response from the card.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer.

This method calls the API function SCardTransmit(). The pointers to the pre-defined / built-in PCI structures are determinated with dlsym() on UNIX/Linux hosts and GetProcAddress() on Windows hosts.

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (SCardPCI ioSendPci, byte[] sendBuffer, SCardPCI ioRecvPci, ref byte[] RecvBuffer)

Parameters

ioSendPci
Structure of Protocol Control Information.
sendBuffer
APDU to send to the card.
ioRecvPci
Structure of protocol information.
RecvBuffer
Response from the card.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. ioSendPci and ioRecvPci are structures containing the following (implemented in PCSC.SCardPCI):
C Example
typedef struct {
	DWORD dwProtocol;    // SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
	DWORD cbPciLength;   // Length of this structure - not used
} SCARD_IO_REQUEST;
  

It is recommended to use pre-defined / built-in PCI structures by calling one of the following methods:

This method calls the API function SCardTransmit(). The pointers to the pre-defined / built-in PCI structures are determinated with dlsym() on UNIX/Linux hosts and GetProcAddress() on Windows hosts.

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (byte[] sendBuffer, int sendBufferLength, byte[] recvBuffer, ref int recvBufferLength)

Parameters

sendBuffer
APDU to send to the card.
sendBufferLength
The buffer size of sendBuffer in bytes.
recvBuffer
Response from the card.
recvBufferLength
The buffer size of recvBuffer in bytes.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. The buffer recvBuffer must be initialized. The size of the returned data will be stored in recvBufferLength. This method will return with error code SCardError.InsufficientBuffer if the buffer size of recvBuffer is too small for the result. If one of the parameters sendBufferLength or recvBufferLength is invalid, the method will throw an ArgumentOutOfRangeException.

This method calls the API function SCardTransmit().

Note:

This method will only work if the reader has been connected with one of the following protocols:
ProtocolDescription
SCardProtocol.T0T=0 active protocol.
SCardProtocol.T1T=1 active protocol.
SCardProtocol.RawRaw active protocol.

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (IntPtr pioSendPci, byte[] sendBuffer, SCardPCI ioRecvPci, ref byte[] recvBuffer)

Parameters

pioSendPci
A pointer to a pre-defined Structure of Protocol Control Information. You can use one of the following:
Protocol Control InformationDescription
SCardPCI.T0Pre-defined T=0 PCI structure. (SCARD_PCI_T0)
SCardPCI.T1Pre-defined T=1 PCI structure. (SCARD_PCI_T1)
SCardPCI.RawPre-defined RAW PCI structure. (SCARD_PCI_RAW)
sendBuffer
APDU to send to the card.
ioRecvPci
Structure of protocol information.
recvBuffer
Response from the card.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. ioRecvPci is a structure containing the following (implemented in PCSC.SCardPCI):
C Example
typedef struct {
	DWORD dwProtocol;    // SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
	DWORD cbPciLength;   // Length of this structure - not used
} SCARD_IO_REQUEST;
  

This method calls the API function SCardTransmit(). The pointers to the pre-defined / built-in PCI structures are determinated with dlsym() on UNIX/Linux hosts and GetProcAddress() on Windows hosts.

C# Example
// Create a new PC/SC context.
SCardContext ctx = new SCardContext();
ctx.Establish(SCardScope.System);

// Connect to the reader
SCardReader RFIDReader = new SCardReader(ctx);
SCardError rc = RFIDReader.Connect(
	"OMNIKEY CardMan 5x21 00 01", 
	SCardShareMode.Shared, 
	SCardProtocol.T1);

if (rc != SCardError.Success) {
	Console.WriteLine("Unable to connect to RFID card / chip. Error: " +
		SCardHelper.StringifyError(rc));
	return;
}

// prepare APDU
byte[] ucByteSend = new byte[] 
	{
		0xFF,   // the instruction class
		0xCA,   // the instruction code 
		0x00,   // parameter to the instruction
		0x00,   // parameter to the instruction
		0x00    // size of I/O transfer
	};
byte[] ucByteReceive = new byte[10];

Console.Out.WriteLine("Retrieving the UID .... ");

rc = RFIDReader.BeginTransaction();
if (rc != SCardError.Success)
	throw new Exception("Could not begin transaction.");

SCardPCI ioreq = new SCardPCI();   // IO returned protocol control information.

rc = RFIDReader.Transmit(
	SCardPCI.T1,/* Protocol control information, T0, T1 and Raw
	             * are global defined protocol header structures. */
	ucByteSend, /* the actual data to be written to the card */
	ioreq,      /* The returned protocol control information */
	ref ucByteReceive);

if (rc == SCardError.Success)
{
	Console.Write("Uid: ");
	for (int i = 0; i < (ucByteReceive.Length); i++)
		Console.Write("{0:X2} ", ucByteReceive[i]);
	Console.WriteLine("");
} 
else 
{
	Console.WriteLine("Error: " + SCardHelper.StringifyError(rc));
}
            
RFIDReader.EndTransaction(SCardReaderDisposition.Leave);
RFIDReader.Disconnect(SCardReaderDisposition.Reset);
  

Requirements

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

Transmit Method

Sends an APDU to the smart card that was previously connected by ISCardReader.Connect(string, SCardShareMode, SCardProtocol).

public SCardError Transmit (IntPtr pioSendPci, byte[] sendBuffer, int sendBufLength, SCardPCI ioRecvPci, byte[] recvBuffer, ref int recvBufLength)

Parameters

pioSendPci
A pointer to a pre-defined Structure of Protocol Control Information. You can use one of the following:
Protocol Control InformationDescription
SCardPCI.T0Pre-defined T=0 PCI structure. (SCARD_PCI_T0)
SCardPCI.T1Pre-defined T=1 PCI structure. (SCARD_PCI_T1)
SCardPCI.RawPre-defined RAW PCI structure. (SCARD_PCI_RAW)
sendBuffer
APDU to send to the card.
sendBufLength
The buffer size of sendBuffer in bytes.
ioRecvPci
Structure of protocol information.
recvBuffer
Response from the card.
recvBufLength
The buffer size of recvBuffer in bytes.

Returns

Return value Description
SCardError.Success Successful (SCARD_S_SUCCESS)
SCardError.InsufficientBuffer sendBuffer or recvBuffer are too big (SCARD_E_INSUFFICIENT_BUFFER)
SCardError.InvalidHandle Invalid card handle (SCARD_E_INVALID_HANDLE)
SCardError.InvalidParameter sendBuffer or recvBuffer or ioSendPci is null (SCARD_E_INVALID_PARAMETER)
SCardError.InvalidValue Invalid Protocol, reader name, etc (SCARD_E_INVALID_VALUE)
SCardError.NoService The server is not runing (SCARD_E_NO_SERVICE)
SCardError.NotTransacted APDU exchange not successful (SCARD_E_NOT_TRANSACTED)
SCardError.ProtocolMismatch Connect protocol is different than desired (SCARD_E_PROTO_MISMATCH)
SCardError.ReaderUnavailable The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCardError.CommunicationError An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCardError.ResetCard The card has been reset by another application (SCARD_W_RESET_CARD)
SCardError.RemovedCard The card has been removed from the reader (SCARD_W_REMOVED_CARD)

Remarks

The card responds from the APDU and stores this response in recvBuffer. The size of the returned data will be stored in recvBuferfLength. This method will return with error code SCardError.InsufficientBuffer if the buffer size of recvBuffer is too small for the result. If one of the parameters sendBufferLength or recvBuferfLength is invalid, the method will throw an ArgumentOutOfRangeException.

This method calls the API function SCardTransmit(). The pointers to the pre-defined / built-in PCI structures are determinated with dlsym() on UNIX/Linux hosts and GetProcAddress() on Windows hosts.

Requirements

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