mortenstarck
mortenstarck

Reputation: 2803

Smart Card Reader, can't read some cards

I have an application that is using an smart card reader for allowing the users to access parts of the system. On one location i have no issues. But another, which have an different type of card manufacturer has a lot of issues. It keeps getting an id of zero back. Then looking into the eventlog i saw this: enter image description here And this is the code:

 card.Connect(reader, SHARE.Shared, PROTOCOL.T0orT1);

 var apduGetID = new APDUCommand(0xFF, 0xCA, 0, 0, null, 4);
 var apduRespGetID = card.Transmit(apduGetID);
long cardId = BitConverter.ToUInt32(apduRespGetID.Data.Reverse().ToArray(), 0);

the second problem is that then trying to debug the code, it works perfect, only then remove the breakpoint can i see the issue but not where. Can some one please help me?

P.S. i found this thread, but it does not work: https://superuser.com/questions/715688/smart-card-errors

Update: Here are the Transmit class

 public override APDUResponse Transmit(APDUCommand ApduCmd)
        {
            var RecvLength = (uint)(ApduCmd.Le + APDUResponse.SW_LENGTH);
            byte[] ApduBuffer;
            var ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH];
            var ioRequest = new SCard_IO_Request
            {
                m_dwProtocol = m_nProtocol,
                m_cbPciLength = 8
            };

            // Build the command APDU
            if (ApduCmd.Data == null)
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)];

                if (ApduCmd.Le != 0)
                {
                    ApduBuffer[4] = ApduCmd.Le;
                }
            }
            else
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length];

                for (var nI = 0; nI < ApduCmd.Data.Length; nI++)
                {
                    ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI];
                }

                ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)ApduCmd.Data.Length;
            }

            ApduBuffer[0] = ApduCmd.Class;
            ApduBuffer[1] = ApduCmd.Ins;
            ApduBuffer[2] = ApduCmd.P1;
            ApduBuffer[3] = ApduCmd.P2;

            m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength);

            if (m_nLastError != 0)
            {
                var msg = "SCardTransmit error: " + m_nLastError;
                throw new SmartCardException(msg, m_nLastError);
            }

            var apduData = new byte[RecvLength];

            for (var nI = 0; nI < RecvLength; nI++)
            {
                apduData[nI] = ApduResponse[nI];
            }

            return new APDUResponse(apduData);
        }

Update 2: I have also tried with to put some Thread.Sleep()

Upvotes: 18

Views: 4336

Answers (2)

Tharif
Tharif

Reputation: 13971

Well if the other system does not take your smart card,just check the BIOS settings for smartcard.There is an option to disable/enable them in some systems.

Upvotes: 0

Samvel Avanesov
Samvel Avanesov

Reputation: 422

Please check that on the second machine you have all the up-to-date drivers of the smart card. Also, sometimes it helps to replace the driver which is provided by the manufacturer with "Microsoft WUDF driver" - https://msdn.microsoft.com/en-us/library/windows/hardware/dn653571(v=vs.85).aspx

Please note, that you have two type of devices detected by the OS when you plug it in - the smart card enumerator device (smart card reader) and the smart card (sometimes called the smart card container) itself. One smart card reader can contain several smart cards.

Example of the smart card which driver was forcefully replaced with Microsoft WUDF to make the client application (iBank2) work:

four smart cards WUDF type

The four smart card drivers have been forcefully replaced with basic Microsoft driver to make the application work.

Upvotes: 3

Related Questions