.NET
  Home arrow .NET arrow Page 3 - Smart Cards in .NET, Part 3
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Smart Cards in .NET, Part 3
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 33
    2004-11-15

    Table of Contents:
  • Smart Cards in .NET, Part 3
  • Two classes to rule them
  • Using the Library: The Demo
  • Smart card resource manager
  • CardCommand and SelectSmart
  • Source code for the library

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Smart Cards in .NET, Part 3 - Using the Library: The Demo


    (Page 3 of 6 )

    You will find the compiled Library and the demo with source code in the zip file for this article. Tracing through the code, you’ll realize how easy it is to use this library. As I’ve not provided any help manual for the compiled library, you’ll have to rely on the intellisence of the VS.NET IDE for prototypes.

    As a reference I’ve included the source code for the SCResMgr Class and the WinSCard.cs file that contains the DllImports to the native functions. They’ll help you understand how the library is implemented.

    The WinSCard.cs file contents are as below:

    using System;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace SCLib
    {
     internal sealed class WinSCard
     {
      // Methods
      public WinSCard()
      {
      }

      // Native Methods Entrypoints
      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardBeginTransaction(IntPtr hCard);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardCancel(IntPtr hContext);

      [DllImport("WINSCARD.DLL", CharSet=CharSet.Auto)]
      internal static extern uint SCardConnect(IntPtr hContext, string szReader, uint dwShareMode, uint dwPreferredProtocols, out IntPtr phCard, out uint pdwActiveProtocol);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardControl(IntPtr hCard, uint dwControlCode, [In] byte[] lpInBuffer, uint nInBufferSize, [In, Out] byte[] lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardDisconnect(IntPtr hCard, uint dwDisposition);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardEndTransaction(IntPtr hCard, uint dwDisposition);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardEstablishContext(uint dwScope, IntPtr pvReserved1, IntPtr pvReserved2, out IntPtr phContext);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardFreeMemory(IntPtr hContext, IntPtr pvMem);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardGetAttrib(IntPtr hCard, uint dwAttrId, ref IntPtr pbAttr, ref uint pcbAttrLen);

      [DllImport("WINSCARD.DLL", CharSet=CharSet.Auto)]
      internal static extern uint SCardGetStatusChange(IntPtr hContext, uint dwTimeout, [In, Out] SCARD_READERSTATE[] rgReaderStates, uint cReaders);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardIsValidContext(IntPtr hContext);

      [DllImport("WINSCARD.DLL", CharSet=CharSet.Auto)]
      internal static extern uint SCardListReaderGroups(IntPtr hContext, ref IntPtr pmszGroups, ref uint pcchGroups);

      [DllImport("WINSCARD.DLL", CharSet=CharSet.Auto)]
      internal static extern uint SCardListReaders(IntPtr hContext, string mszGroups, ref IntPtr pmszReaders, ref uint pcchReaders);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardReconnect(IntPtr hCard, uint dwShareMode, uint dwPreferredProtocols, uint dwInitialization, out uint pdwActiveProtocol);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardReleaseContext(IntPtr hContext);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardSetAttrib(IntPtr hCard, uint dwAttrId, [In] byte[] pbAttr, uint cbAttrLen);

      [DllImport("WINSCARD.DLL", CharSet=CharSet.Auto)]
      internal static extern uint SCardStatus(IntPtr hCard, StringBuilder szReaderName, ref uint pcchReaderLen, out uint pdwState, out uint pdwProtocol, [Out] byte[] pbAtr, ref uint pcbAtrLen);

      [DllImport("WINSCARD.DLL")]
      internal static extern uint SCardTransmit(IntPtr hCard, SCARD_IO_REQUEST pioSendPci, [In] byte[] pbSendBuffer, uint cbSendLength, SCARD_IO_REQUEST pioRecvPci, [In, Out] byte[] pbRecvBuffer, ref uint pcbRecvLength);

      // Private/Internal Types
      [StructLayout(LayoutKind.Sequential)]
      internal class SCARD_IO_REQUEST
      {
       internal uint dwProtocol;
       internal uint cbPciLength;
       public SCARD_IO_REQUEST()
       {
        dwProtocol = 0;
       }
      }

      [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
       internal struct SCARD_READERSTATE
      {
       internal string szReader;
       internal IntPtr pvUserData;
       internal uint dwCurrentState;
       internal uint dwEventState;
       internal uint cbAtr;
       [MarshalAs(UnmanagedType.ByValArray, SizeConst=0x24, ArraySubType=UnmanagedType.U1)]
       internal byte[] rgbAtr;
      }
     }
    }

    More .NET Articles
    More By Digvijay Chauhan


       · Hi,I'm Digvijay .... the author of this article.Please do let me know how you...
       · Article looks very helpful. Will start looking and using them to suit our...
       · Hi,This is basically for managed code that is accessible from all .NET...
       · Hi, I am trying to access memory card (sle4428), as far as I know I need to use...
       · Hi,I jut provided the code to give an overview of how to go about it that is why...
       · Hi,This looks VERY useful. When can we expect the commercial library? I want to...
       · Hi ,You can expect that in March 2005 only.Regards,Digvijay
       · I am attempting to use the code provided and running into a little problem. I have...
       · Hi.In yur first article, what is the SCardLIB 1.0 component?In my Visual Basic...
       · Hi there,This code is quite interesting to me. It works great. Nice work. But I...
       · Hi, the library works fine in some readers, but with the next...
       · hi siri am Ramzy Ebiedi am using C#, ASP.Net VS2005i did test your...
       · Dear DigVijay,I would appreciate if you help me out....I'm developing an...
       · hi,I had this problem too, I tested my application with different cards,some...
       · Hi all,I'm very grateful to Mr. Digvijay for his article. Very interesting.I'm a...
       · Hi all, I'm very grateful to Mr. Digvijay for his article. Very interesting. I'm...
       · How to use your library to write a string like "hello word" and to read from the...
     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway