问题
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 over SCardTransmit if I first connected to a smart card. But as soon as the RF field is off, the card disconnects and I cant send another APDU to turn the field on.
Basically is there any way to send APDU's over pc/sc without a card present. I want to configure certain parts of the reader before reading a card.
Thanks in advance.
-----Edit-----
As FPGA Warrior mentioned I need to use SCardControl to send APDU commands to the card reader.
The steps I've gotten so far:
Connect to 'card' with SCardConnect with SCARD_SHARE_DIRECT and SCARD_PROTOCOL_UNDEFINED. This will return as success and give you the card handle.
_lastError = SCardConnect(_hContext, _cardReaderName, dwShareMode, dwPreferredProtocols, ref phCard, ref _activeProtocol);
Returns: 0 and what looks like a valid _hCard handle.
I then call SCardControl
_lastError = SCardControl(phCard, CTL_code(3500), txBytes, (uint)txByte.Length, out rxBytes, (uint)rxAttr.Length, out rxLen);
I now get the error ERROR_INVALID_HANDLE (0x6). So it seems that my SCardConnect does not return a valid handle, even thou it does not return an error.
As a side note to use PC_to_RDR_Escape mode on the device you might need to edit the registry to turn it on if the drivers you installed does not allow it. https://msdn.microsoft.com/en-us/library/windows/hardware/dn653571%28v=vs.85%29.aspx
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/28539775/sending-apdu-with-winscard-dll-pc-sc-without-a-smart-card-connected