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.
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;
}
}
}