smartcardwinscard

Getting the "name" of the smartcard for SCardGetCardTypeProviderName()


In order to get a crypto context from CryptAcquireContext(), I need to know the provider name for the smartcard currently in the slot.

According to the documentation, SCardGetCardTypeProviderName() will do this, but param 2 is the card name, and I can't see how to determine this non-interactively. All the documentation/examples I've seen either harcode a name or rely on SCardUIDlgSelectCard(), which displays a UI.

Similarly reading around SCardLocateCards(), although it takes a list of smartcard names (for which I'm populating from SCardListCards(), it doesn't seem to pass back anything that identifies the name of the smartcard inserted.

I suspect I'm missing something obvious, but I can't see what.


Solution

  • I was missing something. SCardLocateCards returns as part of the strucutre the ATR of the smartcard, which can then be used in SCardListCards:

    SCARD_READERSTATE smartcardState[MAXIMUM_SMARTCARD_READERS];
    
    result = SCardLocateCards (context, cardsList, smartcardState, readerCount);
    if (result == SCARD_S_SUCCESS) {
        int i;
        for (i = 0; i < readerCount; i++)
        {
            if (SCARD_STATE_ATRMATCH & smartcardState[i].dwEventState) {
                break;
            }
        }
    
        result = SCardListCards (NULL, smartcardState[i].rgbAtr, NULL, NULL, cardName, &count);
        if (result == SCARD_S_SUCCESS) {
            // Cardname for CryptAquireContext in cardName
        }
    }
    

    If anyone has a better route forward let me know - I think this is the only way though from what I can see.