2

For communcation with a Smartcard I use the WINSCARD.DLL as an API to send APDU commands to Smartcards. For a couple of cardreaders this is working as expected, but somehow I get an unkown return (it is not on http://msdn.microsoft.com/en-us/library/ms936965.aspx) value from the method SCardTransmit if I send the command to a O2Micro reader.

What is working: If I send command (values in hex):

CLA: 00, INS: A4, P1: 02, P2: 04, Lc: 02, Data: "4401", Le: (not present)

I get the response SW1: 61. SW2: 1F

The response tells that there are 0x1F bytes available. So I send command:

CLA: 00, INS: A4, P1: 02, P2: 04, Lc: 02, Data: "4401", Le: 1F

But on that command I get no data and return value 0x57.

My question is if anyone knows what the return value 0x57 is telling and maybe a way how to solve or workaround it.

pb2q
  • 58,613
  • 19
  • 146
  • 147
René
  • 129
  • 1
  • 4
  • 14

1 Answers1

4

Your error code is one of the windows System Error Codes from winerror.h: ERROR_INVALID_PARAMETER. This almost always means that your APDUs are ok, but the SCardTransmit arguments are the problem. I recommend looking closely at the pbRecvBuffer parameter. Caveat: I've only used SCard functions with C++, not with C#.

PC/SC functions can return standard windows error codes as well as PC/SC-specific error codes. Note the bit about the FormatMessage call: you can use that to make error reporting a little more generic with predefined error messages supplied by windows.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • I call the function as: `SCardTransmit((uint)Card, IntPtr.Zero , SendBuffer, (uint)SendBuffer.Length, IntPtr.Zero, RecvBuffer, ref RecvLength)` RecvBuffer is a byte array of 257 bytes and RecvLength is 0. I also tested with RecvLength is 257. But still it give me the same error. – René Aug 25 '11 at 12:42
  • 1
    With some effort I found that an invalid prefered protocol was given to the `SCardConnect` method. The parameter `dwPrefProtocol` (the forth in the list) was put as 1|2. Typically it uses 2 in development systems, but 1 on production systems. I changed it hard coding using value 2 and after that it started working. Thanks for helping! – René Sep 02 '11 at 14:00
  • Did you try with 255 bytes max? I believe that's the maximum amount of bytes for that part... – NKCSS Jun 21 '16 at 11:39