From: Ramazzotti... on
Hi All
i am using Asp.net2.0, C#, VS2005
and i am using winscrsd.dll, ScardTransmit. i have this error no. =
2148532228 in return, so please i need your help.

thank you in advance.
this is the code i wrote:


int pContextHandle;
int pCardHandle;
UInt32 byActiveprotocol;
byte[] pbyReaderNames = new byte[1000];
string szReaderName;
uint len = 1000;
//
[DllImport("WINSCARD.DLL")]
static extern int SCardBeginTransaction(int hCard);
//
[DllImport("Winscard.dll")]//
private extern static int SCardEstablishContext(
uint scope,
IntPtr reserved1,
IntPtr reserved2,
out int handle);
//
[DllImport("WINSCARD.dll", EntryPoint = "SCardListReadersA", CharSet =
CharSet.Ansi)]
public static extern int SCardListReaders(
int hContext,
byte[] mszGroups,
byte[] mszReaders,
ref UInt32 pcchReaders);
//
[DllImport("winscard.dll")]
static extern uint SCardTransmit(
int hCard,
ref SCARD_IO_REQUEST pioSendPci,
byte[] pbSendBuffer,
int cbSendLength,
IntPtr pioRecvPci,
byte[] pbRecvBuffer,
ref int pcbRecvLength);
//
[DllImport("winscard.dll", EntryPoint = "SCardConnect", CharSet =
CharSet.Auto)]
static extern int SCardConnect(
int hContext,
[MarshalAs(UnmanagedType.LPTStr)] string szReader,
UInt32 dwShareMode,
UInt32 dwPreferredProtocols,
out int phCard,
out UInt32 pdwActiveProtocol);
[DllImport("Winscard.dll")]
private extern static int SCardDisconnect(UIntPtr cardHandle, int
disposition);

// Private/Internal Types
[StructLayout(LayoutKind.Sequential)]
internal class SCARD_IO_REQUEST
{
internal uint dwProtocol;
internal int cbPciLength;
public SCARD_IO_REQUEST()
{
dwProtocol = 0;
}
}
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName);
[DllImport("kernel32.dll")]
private extern static void FreeLibrary(IntPtr handle);
[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr handle, string
procName);
private static IntPtr GetPciT0()
{
IntPtr handle = LoadLibrary("Winscard.dll");
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci");
FreeLibrary(handle);
return pci;
}
private byte[] GetSendBuffer()
{
string cla = "A0";
string ins = "A4";
string p1 = "00";
string p2 = "00";
string lc = "02";
string body = "3F00";
string script = String.Format("{0}{1}{2}{3}{4}{5}", cla, ins, p1, p2,
lc, body);
byte[] buffer = new byte[script.Length / 2];
for (int i = 0; i < script.Length; i = i + 2)
{
string temp = script.Substring(i, 2);
buffer[i / 2] = byte.Parse(temp,
System.Globalization.NumberStyles.HexNumber);
}
return buffer;
}
public void Transmit()
{
try
{
/*
* for me to use the transmit method as far as i know i need to do this
* 1 - SCardEstablishContext
* 2 - SCardListReaders
* 3 - SCardConnect
* 4 - SCardBeginTransaction
* 5 - SCardTransmit
* 6 - SCardEndTransaction
* 7 - SCardDisconnect
*/
long ret = 0;
ret = SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out
pContextHandle);
ret = SCardListReaders(pContextHandle, null, pbyReaderNames, ref len);
szReaderName = Encoding.ASCII.GetString(pbyReaderNames);
ret = SCardConnect(pContextHandle, szReaderName, 2, (0x00000001 |
0x00000002), out pCardHandle, out byActiveprotocol);
ret = SCardBeginTransaction(pCardHandle);
//
SCARD_IO_REQUEST ioRecv = new SCARD_IO_REQUEST();
byte[] pbRecvBuffer = new byte[255];
int pcbRecvLength = 255;
byte[] pbsendBuffer = this.GetSendBuffer();
int pbsendBufLen = pbsendBuffer.Length;
ioRecv.cbPciLength = ioRecv.ToString().Length;
IntPtr SCARD_PCI_T0 = GetPciT0();
uint uintret = SCardTransmit(pCardHandle, ref ioRecv, pbsendBuffer,
pbsendBufLen, SCARD_PCI_T0, pbRecvBuffer, ref pcbRecvLength);