Smart Cards in .NET, Part 3 - Two classes to rule them
(Page 2 of 6 )
Working along the following lines, we may have two classes: one for resource manager, and another for reader functions, so that they encapsulate the Resource Manager and the Smart Card reader itself. A few helper classes that let you deal with the data exchanged with the card and the commands that you pass to the card can also be added, as I’ve done in our library.
As with any other component, library errors and exceptions are something that we can’t ignore, so I’ve also included a SCardException class, which itself derives from ApplicationException class and provides encapsulation on the response code from the card. Added to that, you can raise an exception based on a Win32 error code that you usually get from the native WinSCard API. Also, an internal sealed class WinSCard provides the primitives for P/Invoking the native APIs, and you may have a look at it in the attached code for this article.
So at this point our classes look like this:

Figure 1 Classes for our Library
The class SCResMgr contains the code to manage Contexts with the Smart Card Resource Manager and lets us use it with an instance of SCReader. Indeed, you can’t create a SCReader without a SCResMgr!
Let’s have a look at the members of the SCResMgr:
public void Cancel()
Is a member Equivalent to native SCardCancel() and the code looks something like the code below:
// Tracking Functions
public void Cancel()
{
if (this.m_hContext == IntPtr.Zero)
{
throw new InvalidOperationException();
}
uint nErrCode = WinSCard.SCardCancel(this.m_hContext);
if (nErrCode != 0)
{
SCardException.RaiseWin32ResponseCode(nErrCode);
}
}
public bool EstablishContext(SCardContextScope nScope)
Is equivalent to SCardEstablishContext and uses encapsulated Context handle. Note how the Context Scope constants in the platform SDK have been grouped in an enum to facilitate their use.
public bool GetStatusChange(int nTimeout, SCStateInfo[] vaSCStates)
Is similar to the SCardGetStatusChange API and this contains three overloads to make its use easier as compared to the native API.
These three examples above will be able to explain to you the style adopted for the library. You will find more details as you get to use the library. For a quick look refer to figure 2, which shows all the members of the SCResMgr library. For a more detailed view, look at the source code listing for the SCResMgr API later in the article.

Figure 2 SCResMgr Class View
Next you may see the Class created for SCReader that encapsulates a Smartcard Reader in figure 3.

Figure 3 The SCReaderClassView
Finally you can see all the classes and Enumerations in the figure below. This code, being a part of a commercial product, can’t be released publicly, but a free compiled version is certainly a strong candidate for release with this article.

Figure 4 The completed library
Next: Using the Library: The Demo >>
More .NET Articles
More By Digvijay Chauhan
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|