Home arrow C++ arrow Page 3 - Writing a Smart Card Library

Writing a Smart Card Library

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.

Author Info:
By: Digvijay Chauhan
Rating: 5 stars5 stars5 stars5 stars5 stars / 116
April 26, 2004
  1. · Writing a Smart Card Library
  2. · The Approach
  3. · The CPCSCReader Class
  4. · The CRegListDlg Class
  5. · The Demo Application: SCardDemo

print this article

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:

Writing a Smart Card Reader
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 StatusCard StatusReader Status
0Reader not opened : FailedCard is not insertedReader is not powered on
1Reader Opened SuccessfullyCard is insertedReader is powered on
3Reader Opened SuccessfullyCard is insertedReader is not powered on
4Reader Opened SuccessfullyCard is insertedreader 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.

Writing a Smart Card Reader
Figure 4: The CRed ListDlg class and Members

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials