I\'m trying to send APDU commands to the card reader itself instead of the Smart Card. The test command I\'m using turns the RF field on and off.
This commands sends ove
I was able to do so using perl binding with those parameters :
#connect witouth card in
$hCard = new Chipcard::PCSC::Card($hContext, $ReadersList[0], $Chipcard::PCSC::SCARD_SHARE_DIRECT, $Chipcard::PCSC::SCARD_PROTOCOL_RAW);
#send any adpu
$cmd = Chipcard::PCSC::ascii_to_array("FF 00 40 F0 04 05 05 03 03");
$hCard->Transmit($cmd);
I had a similar experience calling SCardTransmit after SCardConnect (receiving ERROR_INVALID_HANDLE <0x6> from SCardTransmit).
As an experiment, putting Thread.Sleep(100) after SCardTransmit seemed to change the error code from 0x6 to SUCCESS after the sleep statement (as viewed in debugger). This is not a solution but a hint in the right direction. Writing thread safe code to process the statements after SCardControl would be a better way.
The following link is a good reference:
https://www.csharpstar.com/csharp-race-conditions-in-threading/
I'm not sure why is configuring a reader is made by sending APDUs to a card. It should not be that way. SCardTransmit is for sending command to a card, and it will not work if there is no card (unless you hack the driver so it lies that there is actually a card inserted).
You might be looking for one of these APIs: https://msdn.microsoft.com/en-us/library/windows/desktop/aa375369(v=vs.85).aspx It gives more direct acces to your reader / card.
Specifying that what configurations you wish to set on the reader might increase the change to get an answer that helps you.
Simple way is to only switch on/off Smart card reader is to call SCardEstablishContext for connecting to reader. Note : This will not connect to Smartcard:
/// <summary>
/// Native SCardEstablishContext function from winscard.dll
/// </summary>
/// <param name="dwScope"></param>
/// <param name="pvReserved1"></param>
/// <param name="pvReserved2"></param>
/// <param name="phContext"></param>
/// <returns></returns>
[DllImport("winscard.dll", SetLastError=true)]
internal static extern int SCardEstablishContext(UInt32 dwScope,
IntPtr pvReserved1,
IntPtr pvReserved2,
IntPtr phContext);
To release Reader: this will power off /Release reader handle from the current process. Note : does not have any relation to the smart card.
/// <summary>
/// Native SCardReleaseContext function from winscard.dll
/// </summary>
/// <param name="hContext"></param>
/// <returns></returns>
[DllImport("winscard.dll", SetLastError=true)]
internal static extern int SCardReleaseContext(UInt32 hContext);