0

I have code which checks if smart card is inserted or removed:

void checkCard(void *p)
{
//...
while(true)
{
    if (ReaderState1.dwEventState & SCARD_STATE_EMPTY)
    {
     // Smart card removed, call disconnect
     disconnectCard(cardHandle);

    }
    else
    {
     // Smart card inserted do smth else
    }


}

}

In main I call above thread:

int main()
{
...
    if(establichContext(_hSC) == true)
        {

            // Start thread 
            _beginthread(checkCard, 0, NULL);

            // Sleep
            Sleep(1000000); // or some other logic which halts program for some time

            // Disconnect from card and release context
            disconnectCard(cardHandle);
            releaseContext(_hSC);

        }
}

My problem is if smart card was already removed - by the first code snippet (checkCard function), calling disconnectCard - second time as in main, fails. How would you deal with such situation?

disconnectCard - just uses SCardDisconnect method inside )http://msdn.microsoft.com/en-us/library/windows/desktop/aa379475(v=vs.85).aspx)

4

2 に答える 2

-2

関数でチェックして設定するフラグを用意しdisconnectCardます。

お気に入り

void disconnectCard(someType someArgument)
{
    static bool disconnected = false;

    if (disconnected)
        return;  // Already disconnected

    // ... Do stuff ...

    disconnected = true;
}
于 2013-09-30T07:35:25.530 に答える