C# SCardControl return code 1

3k views Asked by At

This is my first attempt in using a card reader in C#, or basically anywhere.

I use ACS ACR122U PICC Interface 0 reader in Windows 7 64bit.

My first problem occurs when I tried to connect to the reader using

ModWinsCard.SCardConnect(hContext, cbReader.SelectedItem.ToString(), ModWinsCard.SCARD_SHARE_DIRECT, 0, ref hCard, ref Protocol);

It returns error code 6, but I googled and solved it by changing project's platform from Any CPU to X86.

Right after that I bumped to another issue, this time in controlling the reader.

I tried with :

_sentBuffer = new byte[]
    {
         0xFF,
         0x00,
         0x48,
         0x00,
         0x00
    };
_receivedBuffer = new byte[10];

_receivedBuffer[0] = 0;

_returnCode = ModWinsCard.SCardControl(_hCard, _dwControlCode, ref _sentBuffer[0], _sentBuffer.Length, ref _receivedBuffer[0], _receivedBuffer.Length, ref bytesReturned);

The returned code is 1, which is weird because I can't found it in the documentation.

Really need a hand in this.

Thanks !

3

There are 3 answers

0
Samuel Adam On BEST ANSWER

I somehow have it solved by downloading the latest driver from the provider's website, with uninstalling the driver that is included in the driver CD.

Still wonder what does return 1 means though..

1
SuperDre On

I know this is an old topic, but I have the same problem on Windows 10 (x64), except I'm using VB6.

I have a lot working through using SCardTransmit etc, I can controle/read/write if there's a card on it. But I want to control the reader (ACR112U) without a card (turn off autodetection/beep) and that's only possible using SCardControl (as far as I know), cause when I connect with Directmode and then call our SetBuzzerCardDetection to turn it off, I get a ERROR_BAD_COMMAND, but when shared and a card it works.

Even the example from ACS themselves with IOCTL_GET_VERSIONS gives me return value ERROR_INVALID_FUNCTION

  Dim vcVersion       As VERSION_CONTROL
  Dim lReturnedLength As Long

  m_lResult = SCardControl(m_hCard, _
                           IOCTL_GET_VERSIONS, _
                           0, 0, _
                           vcVersion, 20, _
                           lReturnedLength)
  If m_lResult <> SCARD_S_SUCCESS Then

I'm using the following

Declare Function SCardControl Lib "WinScard.dll" ( ByVal hCard As Long, ByVal dwControlCode As Long, ByRef lpInBuffer As Any, ByVal lSizeofBuffer As Long, ByRef lpReceiveBuffer As Any, ByVal lpReceiveBufferSize As Long, ByRef lpBytesReturned As Long) As Long

Const IOCTL_CCID_ESCAPE         As Long = (&H42000000 + 3500)

'hCard is a valid handle returned by SCardConnect
'lReturn = SCardConnect(hContext, sReader, eShareMode, ePreferredProtocol, hCard, eActiveProtocol)
'With card on reader:    eShareMode = SCARD_SHARE_SHARED, ePrefferedProtocol = SCARD_PROTOCOL_Tx
'Without card on reader: eShareMode = SCARD_SHARE_DIRECT, ePrefferedProtocol = SCARD_PROTOCOL_UNDEFINED

Dim abIn()          As Byte
Dim abOut()         As Byte
Dim lInLength       As Long
Dim lOutLength      As Long
Dim lReturn         As Long
Dim lReturnedLength As Long
  
'Command for turning off Buzzer output during card detection
lInLength = 5
ReDim abIn(lInLength - 1)  
abIn(0) = &HFF
abIn(1) = &H0
abIn(2) = &H52
abIn(3) = &H0
abIn(4) = &H0

lOutLength = 256
ReDim abOut(lOutLength - 1)

lReturn = SCardControlAny(hCard, _
                          IOCTL_CCID_ESCAPE, _
                          abIn(0), lInLength, _
                          abOut(0), lOutLength, _
                          lReturnedLength)
'with card on reader:    lReturn = ERROR_BAD_COMMAND
'without card on reader: lReturn = ERROR_INVALID_FUNCTION

Mind you for using Escape I also added the ControlEscapeEnable to the registry (just for the sake of it I added it in several places) First I did everything without loading a special driver (other than what Windows 10 itself already got), but due to Samuel Adam's remark I loaded the latest from ACS website but it didn't make a difference. It's really driving me crazy, just like it's also driving me crazy that WebUSB doesn't allow it anymore so I would have been able to use a webbased version.

3
sbaker On

Doing some research myself about working with SCardControl and found I was getting the same return value of 1.

I found a list of Error Codes here which then states the below.

"Note Some return values may have the same value as existing Windows return values that signify a similar condition. For information about error codes not listed here, see System Error Codes."

And that documentation states that the error code value 1 is ERROR_INVALID_FUNCTION

I know this question is old but hopefully it will help someone in the future.