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 Demo Application: SCardDemo (Page 5 of 5 )
Figure 6:The SCardDemo application snapshot
The demo application demonstrates the use if these classes and you can even use it as a starting point for your Smart Card enabled application. It allows you to first select a reader from the available ones and then fires one command automatically and displays you the response in a Messagebox. Once you've selected a reader you can just type in an APDU in the Command String field and then press the Send Button to sent it to the card and retrieve the response in the Response field as displayed in the snapshot above.
The Library Code
The Library code is in two modules, namely DSCPCSC.dll and DSCPCSCOO.dll.
The DSCPCSC.dll is a pure Win32 DLL and exports four APIs
int WINAPI OpenReaderConnection(char* strNameReader, long status)
int WINAPI CloseReaderConnection(char* strNameReader, long status)
long WINAPI SendCommand(char * strReader, unsigned char* strCommand, int nLengthCmd, long status, unsigned char & strResponse, int & nResponseLen)
long WINAPI GetReaderAttribute(char* strReaderName, unsigned long attId, unsigned char& pAttribute, int nAttLength)
These are the core APIs that do the real work and for a better understanding I'd recommend you to browse through the code once.
The other module, DSCPCSCOO.dll, is a MFC extension library and exports all the classes that we discussed above. As a refresher I'll list them again here.
CPCSCReader The Reader Class, which encapsulates the functionality of a Smart Card Reader.
CPCSCCommand The APDU or Command Object which encapsulates the APDU Commands functionality that are sent to the card.
CRegListDlg This class encapsulates the UI to select a reader from the installed readers on the system.
Points of interest
The first module is a Pure Win32 library and as I developed the second module DSCPCSCOO.dll as an OO wrapper over it one can use JNI to develop a java wrapper over it and even write Java Applications that re Smart Card enabled.
I hope this would give some of the readers a good introduction to MFC Smart Card programming and will prove to be a starting point for others. In case of any questions regarding this article you can mail me at Digvijay_Chauhan@hotmail.com.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.