How do I get a card serial number? [Pulled]

  c++, winscard

I have a card reader left at home, so I want to use it, but there is a problem.

How do I get a card serial number by smart card reader? in window os using by winscard library.

It probably uses "SCardTransmit" function, is that right?

I’ve looked at it, but I don’t understand the process.

link : https://docs.microsoft.com/ko-kr/windows/win32/api/winscard/

Error occurred : 1734 (Array range is invalid)

CARD NUMBER EXAMPLE IMAGE

cmake_minimum_required(VERSION 3.19)
project(smart_card_reader)

set(CMAKE_CXX_STANDARD 14)

link_directories( "C:/Program%20Files%20(x86)/Windows%20Kits/10/Lib/10.0.18362.0/um/x64" )
link_libraries( winscard )
INCLUDE_DIRECTORIES( "A:/mingw_w64/mingw32/i686-w64-mingw32/include" )
add_executable(smart_card_reader main.cpp)
#include <iostream>
#include <windows.h>
#include <vector>

bool test() {

    SCARDCONTEXT hSC;
    LONG lReturn_handle;

    lReturn_handle = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hSC);

    if (SCARD_S_SUCCESS != lReturn_handle)
        printf("Failed SCardEstablishContextn");
    else {

        printf("Success Established context!n");

        SCARDHANDLE hCardHandle;
        LONG return_card_connection_context(0);
        DWORD dwAP;
        return_card_connection_context = SCardConnectA(
                hSC
                , (LPCTSTR)"HST USB Smart Card Reader 0"
                , SCARD_SHARE_SHARED
                , SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
                , &hCardHandle
                , &dwAP);


        if (0x6 == return_card_connection_context) {
            printf("INVAILED HANDLE ERRORn");
        };

        if ( SCARD_S_SUCCESS != return_card_connection_context )
        {
            printf("Failed SCardConnectn");
            std::cout << return_card_connection_context << " / " << hCardHandle << " / " << dwAP << 'n';
            exit(1);  // Or other appropriate action.
        };




        LPBYTE   pbAttr = NULL;
        DWORD    cBytec = SCARD_AUTOALLOCATE;
        DWORD    ic;
        LONG     lReturnc;

        lReturnc = SCardGetAttrib(hCardHandle,
                                  SCARD_ATTR_DEVICE_SYSTEM_NAME,
                                 (LPBYTE)&pbAttr,
                                 &cBytec);
        if ( SCARD_S_SUCCESS != lReturnc )
        {
            if ( ERROR_NOT_SUPPORTED == lReturnc )
                printf("Value not supportedn");
            else
            {
                // Some other error occurred.
                printf("Failed SCardGetAttrib - %xn", lReturnc);
                exit(1);  // Or other appropriate action
            }
        }
        else
        {
            // Output the bytes.
            std::cout << cBytec << 'n';
            for (ic = 0; ic < cBytec; ic++)
                printf("%c", *(pbAttr+ic));
            printf("n");

            // Free the memory when done.
            // hContext was set earlier by SCardEstablishContext
            lReturn_handle = SCardFreeMemory( lReturn_handle, pbAttr );
        }






        WCHAR           szReader[200];
        DWORD           cch = 200;
        BYTE            bAttr[32];
        DWORD           cByte = 32;
        DWORD           dwState, dwProtocol;
        LONG            lReturn;

        lReturn = SCardStatus(hCardHandle,
                              (LPSTR)szReader,
                              &cch,
                              &dwState,
                              &dwProtocol,
                              (LPBYTE)&bAttr,
                              &cByte);

        if ( SCARD_S_SUCCESS != lReturn )
        {
            printf("Failed SCardStatusn");
            exit(1);
        }


        printf("%Sn", szReader );
        switch ( dwState )
        {
            case SCARD_ABSENT:
                printf("Card absent.n");
                break;
            case SCARD_PRESENT:
                printf("Card present.n");
                break;
            case SCARD_SWALLOWED:
                printf("Card swallowed.n");
                break;
            case SCARD_POWERED:
                printf("Card has power.n");
                break;
            case SCARD_NEGOTIABLE:
                printf("Card reset and waiting PTS negotiation.n");
                break;
            case SCARD_SPECIFIC:
                printf("Card has specific communication protocols set.n");
                break;
            default:
                printf("Unknown or unexpected card state.n");
                break;
        };



        // Use the connection.
        // Display the active protocol.
        switch (dwAP) {
            case SCARD_PROTOCOL_T0:
                printf("Active protocol T0n");
                {
                    SCARD_IO_REQUEST pioRecvPci;

                    DWORD dwActiveProtocol, dwSendLength, dwRecvLength;
                    BYTE pbRecvBuffer[16];
                    BYTE pbSendBuffer[] = {(BYTE)0xFC, (BYTE)0xB0,(BYTE)0x05, (BYTE)0x00, (BYTE)0x00};

                    dwSendLength = sizeof(pbSendBuffer);
                    dwRecvLength = sizeof(pbRecvBuffer);

                    SCARD_IO_REQUEST pioSendPci;

                    pioSendPci.dwProtocol=SCARD_PROTOCOL_T0;
                    pioSendPci.cbPciLength= sizeof(pioSendPci);

                    SCARD_IO_REQUEST ioxRecv;
                    ioxRecv.cbPciLength = 255;

                    LONG ret = SCardTransmit(hCardHandle,
                                        &pioSendPci,
                                        pbSendBuffer, dwSendLength,
                                             &ioxRecv, pbRecvBuffer, &dwRecvLength);
                    std::cout << "result : " << ret << " / send : " << pbSendBuffer << "*" << dwSendLength << " / " << "receive : " << pbRecvBuffer << "*" << dwRecvLength << 'n';


                    //HOW DO I GET CARD NUMBER?

                };
                break;

            case SCARD_PROTOCOL_T1:
                printf("Active protocol T1n");
                break;

            case SCARD_PROTOCOL_UNDEFINED:
            default:
                printf("Active protocol unnegotiated or unknownn");
                break;
        }

    };

    return false;
};
int main() {


    std::cout << "Hello, World! -- smart card reader" << std::endl;

    test();

    return 0;
}

Hello, World! -- smart card reader
Success Established context!
28
HST USB Smart Card Reader 0 

Card has specific communication protocols set.
Active protocol T0
result : 0 / in send : ��*5 / in receive : m*2

Process finished with exit code 0

Source: Windows Questions C++

LEAVE A COMMENT