Tx Systems, Inc. Logo - Mid

Tx Systems, Inc. Logo - Bottom

PC/SC Development
Introduction

In a nutshell PC/SC is a set of card and reader specifications that allow a programmer to write code that will be reader independent and in some cases OS independent.

This page is intended as a quick start guide for programmers who want to write code that access a card using PC/SC.

If you would like to learn more about PC/SC or specifications for cards and readers please visit The PC/SC Workgroup.

This page works off of a small stripped down PC/SC application built in Visual C++. You may download TestPCSC-0.1.zip here.

If your PC/SC development is using Visual Basic please download this project. VB PCSC Sample.zip

This application has been written to be a simple very basic way to examine PC/SC. Unzip it and open it up in Visual Studio to compile and run your first PC/SC application. You can use it as a starting point for your application but please be aware that it was designed for simplicity and does not necessarily demonstrate proper programming techniques.

For detailed information on the programming interface for PC/SC you can visit the MSDN page for Smart Card and Reader Access Functions.


Project Design

This Visual C++ project impliments a simple class called SCardManager. This class defines routines for all of the major PC/SC functions and does error checking for each. This class also has member variables to store the PC/SC reader list and a routine to convert PC/SC error codes into text representations.

Class Variables:
m_csReaderList a string containing a list of null terminated reader strings set by a call to m_SCardListReaders() (SCardListReaders)
m_hSC the SCARD Context (connection to a reader) set by a call to m_SCardEstablishContext( (SCardEstablishContext). This handle is used in subsequent PC/SC calls.
m_hCardHandle the SCard Handle (connection to a card) set by a call to m_SCardConnect (SCardConnect). This handle is used in subsequent communication with the card.
m_SelectedReader this is a member variable we set aside to store the user selected reader. In PC/SC it is very easy to support multiple readers attached to a single computer and the user can select to which reader they would like to connect.

Class Variables:

Many of the functions in this module are just wrappers around their counterpart in PC/SC, we add error checking to the class version. The PC/SC counterpart, if any, is shown in the table below.

m_SCardEstablishContext a wrapper around it's PC/SC counterpart SCardEstablishContext this is the first call made when setting up PC/SC communication. We call this function on object creation. For more information on SCardEstablishContext, options and return values please see the MSDN documentation for SCardEstablishContext.
m_SCardListReaders a wrapper around it's PC/SC counterpart SCardListReaders this fuction gets a list of PC/SC readers from the PC/SC Resource Manager and initializes our member variable m_csReaderList. For more information on SCardListReaders please see the MSDN documentation for SCardListReaders.
m_SCardConnect a wrapper around it's PC/SC counterpart SCardConnect this function takes the user selected reader string and opens a connection. We use some basic options for this function SCARD_SHARE_SHARED means this application is willing to share the card with other applications, and the SCARD_PROTOCOL* list is just letting the resource manager know what protocols we will accept. For a complete list of options please see the MSDN documentation for SCardConnect.
m_SCardReleaseContext a wrapper around it's PC/SC counterpart SCardReleaseContext this function lets the PC/SC resource manager know that we are done acessing the reader and frees up any smart card contexts he had established. This call should always be made if an SCardEstablishContext has succeeded and hence we put it in our object destructor. For more information please see the MSDN Documentation for SCardReleaseContext
m_GetPCSCErrorString This function takes a PC/SC error code (LONG) and returns a string representation of that error code. Very useful for debugging.
M_FillReaderComboBox This function takes a pointer to a dialog combobox and fills it with readers from the member object m_csReaderList.

Sample Run

This sample application is designed to tell you what it is doing each step along the way in order to help you understand the PC/SC process.

Any PC/SC application will follow this set of steps:

1. Establish a PC/SC context with a call to SCardEstablishContext. In our application we let you know whenever a context is being established with a message box:
PCSC SCardEstablishContext Image

2. Get a list of readers with SCardListReaders and populate the reader list.
PCSC SCardListReaders Image

3. Connect to a specific reader with a call to SCardConnect
PCSC SCardConnect Image

4. Handle communication with your card. In our application we just display the active protocol.
PCSC Active Protocol Image


Where To From Here

This example was designed to give you a basic understanding of how to setup PC/SC communication with a reader and card. This is a great starting point, however it leaves out the most important part, card communication. We have left this out purposfully because every card communicates differently.

In order to actually communicate with the card you will need to issue calls to SCartTransmit. This will most likely take the form of composing and sending APDU's. An APDU is basically a 'function call' to the card, complete with input, output and return values. If you are not familiar with APDU's and the APDU's for your specific card please ask the card manufacturer for card specifications. If you are a neophyte in the world of smart card programming we also recommend you get a basic book on smart cards in order to better understand how things work.

There is also a website called Smart Card Basics that is a very good starting point for understanding how smarct cards work.

Hopfully this application has provided a starting point for you in your PC/SC development, allowing you to get a basic understanding of how easy it can be to access smart cards from any PC/SC compliant reader.


 

Copyright © 2004-2005 Tx Systems, Inc. All rights reserved.