In this article we are going to develop a Smart Card Library that will ease the development of Smart Card Applications using the Win32 SDK and/or MFC. This assumes that you’re familiar with Win32 and MFC. The library builds upon the available support for Smart Cards in Windows by providing an Object Oriented wrapper over the WinSCard API. One advantage of using this library is the layered approach, which isolates the core API’s available to non-Win32 conformant languages like java to access this library through JNI (Java Native Interfaces). If you’ve already developed some applications using the APIs exposed by WinSCard.dll then you’ll be well aware of the complexities involved and here is an attempt to ease them.
Writing a Smart Card Library - The CPCSCReader Class (Page 3 of 5 )
The class snapshot above gives us the clear picture of what it encapsulates and what methods we can invoke to communicate with the physical reader and the inserted card. In order to open a physical reader you must know the name with which it's configured. I've assumed in code that it's stored in the registry at the key:
HKEY_CURRENT_USERDSCPCSCReader as in the snapshot below:
Figure 3:The CPCSReader class and Members
You can always modify the code to suit your need and requirements. It's just meant to be a demonstration and you are free to evolve it in any way you like. You can double click the RegReader.reg file to make the keys and alter them as you like.
CPCSCReader members
int OpenReader(CString&) This method opens the reader specified by the CString parameter, which holds the name of the reader to be opened. The return value is an int with the following meanings
Call Status
Card Status
Reader Status
0
Reader not opened : Failed
Card is not inserted
Reader is not powered on
1
Reader Opened Successfully
Card is inserted
Reader is powered on
3
Reader Opened Successfully
Card is inserted
Reader is not powered on
4
Reader Opened Successfully
Card is inserted
reader is powered on
int CloseReader(CString& strReaderName) It Closes the Reader Opened earlier by a call to OpenReader.The return value is either 0 or 1 indicating success or failure. Zero means failure because the Context could not be released and one means that the context was released successfully and that the reader closed successfully and it also asserts that that no card is present and reader is not powered on.
void SendCommandToCard(CPCSCCommand&) This is the workhorse of the whole library and wraps the raw API SCardTransmit. It takes CPCSCCommand reference as parameter and the response is returned in that too.
BOOL SendCommandToCard(CString&, CString&) Same as above but for ease of use takes a CString as Command APDU and returns the response and Status words in another CString.
void GetReaderStatus() It's not actually implemented but one should send a 00 60 10 00 00 as Command and interpret the response from Card.
BOOL IsCardPresent() Returns true if Card is inserted in the reader else returns false.
BOOL IsReaderPowerd() Returns true if Reader is powered on else returns false.
CString m_strReaderName Holds the Reader name with which it was opened. See OpenReader() code for more details.
int m_bReaderPowerdOn This is Boolean member variable to hold the status of power of reader.
int m_bCardInserted; This is Boolean member variable to hold the card presence status of reader.