Calling SCardGetStatusChange
There seems to be a lot of confusion over just how to use the PC/SC
SCardGetStatusChange service. Here is an example of its proper use on
Windows.
CardWatch.cpp
Note the following:
- First off, there's a special outer loop that uses an undocumented
feature of the Windows PC/SC implementation -- the special reader name
"\\?PNP?\Notification" gets events when smart card reader Plug 'n Play
events occur. It is used in an outer loop to rebuild the list of
readers being watched.
- Within the inner loop, a DWORD named dwChanges is derived. This
DWORD contains flags that indicate what event has occurred. For
example, if the bit SCARD_STATE_PRESENT is set in dwChanges, then that means
a change has occurred in the card present state for that reader.
Then your code would look at the SCARD_STATE_PRESENT bit in pRdr->dwEventState
to see if the change is that a card became present, or a card was removed.
- Most confusion over the use of this call comes from getting too involved
in the undefined bit states. They're there only to help the Smart Card
Subsystem -- your code shouldn't need to look at them!
A lot of people ask why it does things this way instead of using <insert your
favorite event mechanism here>. The reason is that when PC/SC designed it,
we wanted it to be useable on multiple platforms. The only common event
mechanism we could find was to simply use threads. Hence it assumes
threads, and blocks until something happens.