5

I've had this software running in production for years and never seen this problem before. I just received a new laptop (HP EliteBook 8470p) that has a built-in Alcor Micro USB Smart Card Reader.

The code below will list all readers on a system and seems to work fine. Some of our systems will have 3 or 4 readers plugged in to a single computer. It's been tested with a dozen or so models with no issues.

Strangely, the Alcor reader will only get listed when a Smart Card is inserted. If I look at it in Device Manager, it doesn't show up under the "Smart card readers" until a card is inserted, too (unless I go to View > Show Hidden Devices).

Does anyone know why this is or if there's a way to make sure it gets listed in my software?

Thank you.

The code:

[DllImport("WINSCARD.DLL", EntryPoint = "SCardEstablishContext", CharSet = CharSet.Unicode, SetLastError = true)]
static internal extern uint EstablishContext(ScopeOption scope, IntPtr reserved1,
    IntPtr reserved2, ref SmartcardContextSafeHandle context);

[DllImport("WINSCARD.DLL", EntryPoint = "SCardListReaders", CharSet = CharSet.Unicode, SetLastError = true)]
static internal extern uint ListReaders(SmartcardContextSafeHandle context, string groups,
    string readers, ref int size);

private bool EstablishContext()
{
    if ((this.HasContext))
    {
        return true;
    }
    this._lastErrorCode =
        (SmartcardErrorCode)UnsafeNativeMethods.EstablishContext(ScopeOption.System,
        IntPtr.Zero, IntPtr.Zero, ref this._context);
    return (this._lastErrorCode == SmartcardErrorCode.None);
}

public ArrayList ListReaders()
{
    ArrayList result = new ArrayList();

    //Make sure a context has been established before 
    //retrieving the list of smartcard readers.
    if (this.EstablishContext())
    {
        //Ask for the size of the buffer first.
        int size = this.GetReaderListBufferSize();
        //Allocate a string of the proper size in which 
        //to store the list of smartcard readers.
        string readerList = new string('\0', size);
        //Retrieve the list of smartcard readers.
        this._lastErrorCode =
            (SmartcardErrorCode)UnsafeNativeMethods.ListReaders(this._context,
            null, readerList, ref size);

        if ((this._lastErrorCode == SmartcardErrorCode.None))
        {
            //Extract each reader from the returned list.
            //The readerList string will contain a multi-string of 
            //the reader names, i.e. they are seperated by 0x00 
            //characters.
            string readerName = string.Empty;
            for (int i = 0; i <= readerList.Length - 1; i++)
            {
                if ((readerList[i] == '\0'))
                {
                    if ((readerName.Length > 0))
                    {
                        //We have a smartcard reader's name.
                        result.Add(readerName);
                        readerName = string.Empty;
                    }
                }
                else
                {
                    //Append the found character.
                    readerName += new string(readerList[i], 1);
                }
            }
        }
    }
    return result;
}

Btw, this code was written by someone else who I'm guessing (by the over-abundance of comments) found it somewhere else online. I'm somewhat familiar with it but never gotten too deep into it. I've tried making several tweaks to it and can't get it to list that Alcor reader at all.

Thanks!

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • Never observed such an effect with the readers here. For a contactless reader this would be especially nasty. I would complain at Alcor or HP to get a driver update, since the only alternative would be to descend from winscard.dll (i.e. resource manager) level to USB devices enumeration. – guidot Apr 12 '13 at 07:01
  • @guidot Thank you for your response. Yeah I've been working with readers for years and never seen this behavior either. As I said, it does the same thing in Device Manager in Windows - it shows as inactive until a card is inserted so it definitely seems to be something at the driver/hardware level and not my code... I think I'll take your advice and send Alcor an email. – Adam Plocher Apr 12 '13 at 16:55
  • Check the smartcard services running or not......... – jiten Apr 15 '13 at 07:29
  • It is running. I have a couple of external USB readers plugged in (one via a printer, and one stand-alone). Those two show up, but the internal one on my laptop doesn't UNTIL I insert a smart card. It behaves this way in Device Manager too - it shows as an inactive card reader until I insert a card - as if the USB was unplugged (although it's an internal USB card reader in my laptop). It's something to do with this manufacturer/model I think. I sent them an email a few days ago and haven't heard back... – Adam Plocher Apr 15 '13 at 07:54
  • That's very nasty behavior. I would certainly complain. Sounds like the driver devs opted for a shortcut. – Maarten Bodewes Apr 16 '13 at 23:43
  • Well I tried contacting them about a month ago and still no word back. I'm going to open a bounty on this and see if anyone has had any experience with this. Thanks – Adam Plocher May 14 '13 at 20:33

1 Answers1

4

Ok, I feel really stupid finding the answer immediately after opening the bounty. I spent a while looking at this from a software perspective and gave up for a while - when I came back to revisit this I decided it might be suited for a bounty.

I decided to take a closer look at my BIOS options, and guess what? There's an option in there that says "Power on Smart Card Reader: a) when card is inserted, b) always". I changed it to "Always" and it works. ARGH

It won't let me delete my question since it has a bounty now, but that's basically my answer. Thanks for the comments/advice.

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • Thank you so much for posting this. You saved me hours, wondering why it was doing this and why my code that was working now does not. – darbid Dec 15 '18 at 19:27
  • Also see [How to enumerate certificates on Smart Card](https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/41d042ac-4eaf-40e6-9f90-5072a14142b6/how-to-enumerate-certificates-on-smart-card) on MSDN and the [chronic] Microsoft bug that is discussed. – jww Sep 24 '19 at 04:13