CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2003
    Location
    India
    Posts
    93

    Question Smart Card Reset

    I am working on an application which communicates with a smartcard.. I am using Platform SDK for doing this program. Well, my problem is, when i try to use SCardConnect (), it ruturns an error "SCARD_W_UNRESPONSIVE_CARD" which means The smart card is not responding to a reset. Why this error occurs. Can anybody help me telling which all are the situations the card can not responds to a reset ? I am getting the resourse manager context , but not connecting with the card even though i have inserted the card in the reader.

    Note: I have an application that is given by the smart card providers.. if i run that program and terminate that application by not disconnecting the card from the reader, then my VC++ application does not show SCARD_W_UNRESPONSIVE_CARD error.. Why is it so ?

    Thank you all.

  2. #2
    Join Date
    Oct 2003
    Location
    India
    Posts
    93
    Which all are the situations when a smart card does not responds to a reset and how can we make a card respond to a reset ?
    how to solve this condition ?
    Thank you all.

  3. #3
    Join Date
    Aug 2003
    Posts
    82
    The only thing I can say is check the MSDN Smart Card API

  4. #4
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Can you show the peice of code that doesn't work?

    Is it user or system scope in the SCardEstablishContext call? What dwShareMode flag do you use when calling ScardConnect?

    The best way for you to let us help you, is to provide the peice of code that fails + more about your environment: card reader maker and operating system version.

    Also, your post should probably have been posted in "C++ and WinAPI" forum (http://www.codeguru.com/forum/forumd...ps=&forumid=47) instead.

  5. #5
    Join Date
    Oct 2003
    Location
    India
    Posts
    93

    Post Details

    I am working on windows 98 CE , and the my smart card reader is "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0". The smart card providers have given an API which deals with Memory Cards, but at the same time, the sample code they have provided is based on Windows SCard API. So, i tried to make an application using Windows SCard API. Now When i run the sample program that the providers have given, it run with out many problem. However it have some problems while i insert card on the reader.

    Well, when i write the same code in my program, the program doesnt work. When i try to connect , it shows "SCARD_E_UNKNOWN_READER". I am using SCardEstablishContext in the following way...
    ------------------------------------------------------------------------------------
    void CSCardTest1Dlg::OnEstablishContext()
    {

    LONG lReturn;
    lReturn=SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&hSC);
    if(lReturn==SCARD_S_SUCCESS)
    {
    MessageBox("Established Resource Manger Context","Sucess");
    }
    else
    {
    MessageBox("There occured some errors","Opps");
    }
    }
    ----------------------------------------------------------------------------------

    And SCardConnect is,
    -----------------------------------------------------------------------------------
    void CSCardTest1Dlg::OnConnectCard()
    {

    LONG lReturn;
    DWORD dwAP;

    // Connect to the reader.
    // hContext is a SCARDCONTEXT previously set by
    // a call to SCardEstablishContext.
    lReturn = SCardConnect( hSC,pReader,SCARD_SHARE_DIRECT,NULL,&hCardHandle,&dwAP );
    if ( SCARD_S_SUCCESS != lReturn )
    {
    MessageBox("**** !!! Failed SCardConnect\n"+m_GetErrorString(lReturn));
    }
    else
    {

    switch ( dwAP )
    {
    case SCARD_PROTOCOL_T0:
    MessageBox("Active protocol T0\n");
    break;

    case SCARD_PROTOCOL_T1:
    MessageBox("Active protocol T1\n");
    break;

    case SCARD_PROTOCOL_UNDEFINED:
    default:
    MessageBox("Active protocol unnegotiated or unknown\n");
    break;
    }
    }

    }
    ---------------------------------------------------------------------------------

    I have installed the driver for this particular reader, so i think i dont have to introduce this reader to the system programatically..
    If i change the Mode and Protocol while calling SCardConnect, it shows different errors. Can anyone give some solution or an idea why this is happening with my code only, but runs ok with the sample code provided by the Smard card providers.

    Thank you all.

  6. #6
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Some things to verify:

    1. Verify that pReader is defined and assigned as follows:
    LPCTSTR pReader = L"SCM Microsystems Inc. SCR33x USB Smart Card Reader 0";

    2. Are you sure about the above reader name? Try enumerate all readers, just to make sure you're using the correct name.

    3. The reset problem can be that you've set dwPreferredProtocols to 0. Try setting it to SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 instead.

  7. #7
    Join Date
    Oct 2003
    Location
    India
    Posts
    93
    I have loaded my reader name through program only.... the code for that is...
    --------------------------------------------------------
    void CSCardTest1Dlg::OnGetDetails()
    {
    LPTSTR pmszReaders = NULL;

    LONG lReturn, lReturn2;
    DWORD cch = SCARD_AUTOALLOCATE;

    // Retrieve the list the readers.
    // hSC was set by a previous call to SCardEstablishContext.
    lReturn = SCardListReaders(hSC,NULL,(LPTSTR)&pmszReaders,&cch );
    char b[100];
    wsprintf(b,"%ld",lReturn);

    switch( lReturn )
    {
    case SCARD_E_NO_READERS_AVAILABLE:
    MessageBox("Reader is not in groups.\n");
    // Take appropriate action.
    // ...
    break;


    case SCARD_S_SUCCESS:
    // Do something with the multi string of readers.
    // Here, we'll merely output the values.
    // A double-null terminates the list of values.
    pReader = pmszReaders;
    while ( '\0' != *pReader )
    {
    // Display the value.
    MessageBox(pReader );
    // Advance to the next value.
    pReader = pReader + strlen(pReader) + 1;
    }
    // Free the memory.
    lReturn2 = SCardFreeMemory( hSC,pmszReaders );
    if ( SCARD_S_SUCCESS != lReturn2 )
    MessageBox("Failed SCardFreeMemory\n");
    break;

    default:
    MessageBox("Failed SCardListReaders\n");
    // Take appropriate action.
    // ...
    break;
    }


    }
    -----------------------------------------------------------------------------
    And i have tried using protocol like this too
    SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 , but it showed error....

    why so ?
    Thank you all.

  8. #8
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Getting SCARD_E_UNKNOWN_READER back from SCardConnect() must mean that Resource Manager cannot find any reader with that name.

    If you're 100 % sure that "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0" is one of the names when enum all readers, let's hardcode it when calling SCardConnect():
    Code:
    lReturn = SCardConnect(hSC,pReader,
                    L"SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
                    SCARD_SHARE_DIRECT,
                    SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
                   &hCardHandle,&dwAP);
    Try the above statement.

  9. #9
    Join Date
    Oct 2003
    Location
    India
    Posts
    93

    yes it worked

    Thank you so much j0nas.. i figured out the problem with your help. The reader name was not correct. Now i have corrected it and its working. I really appreciate your help.

    Now its connecting properly but the only problem is I can connect only in SCARD_SHARE_DIRECT mode with NULL protocol... everything else ( all other modes and protocols) doesnt work with this Card. It says SCARD_W_UNRESPONSIVE_CARD when i try with another modes and protocols...what must be the reason for it ? Then one other thing i have noticed is, it doesn't return the ATR ( Answer to reset) for the card. Why so ? But, i have a application which is provided by the card manufacturers.. when i run that application and end that application with out disconnecting the card.. then, my program returns ATR... that means, when i run the application given by the card providers, it sets something for the card and after that all other application communicates with card can detect ATR and other attributes...

    what must be the reason and how can i set that ?
    Thank you all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured