WinScard.dll, ScardTransmit

  • Thread starter Thread starter Ramazzotti...
  • Start date Start date
R

Ramazzotti...

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);
 
I'll give this a try...

- Opening up calc & converting 214532228 to hex yields --> 0x8010 0004
- SmartCard.h lists the following error code
//
// MessageId: SCARD_E_INVALID_PARAMETER
//
// MessageText:
//
// One or more of the supplied parameters could not be properly
interpreted.
//
#define SCARD_E_INVALID_PARAMETER ((DWORD)0x80100004L)

Here's a quick stab at an answer:
After looking at your code, you may want to check your define of
SCardTransmit try changing the int you are marshalling for hCard to
something else. Similarly, that would require a change to your
SCardConnect.

My reasoning:
winscard.h defines the following:
typedef ULONG_PTR SCARDHANDLE;

Good Luck


Ramazzotti... said:
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);
 
thank you for your reply
i am using C# VS.net 2005
just to let you know
thank you

Functional said:
I'll give this a try...

- Opening up calc & converting 214532228 to hex yields --> 0x8010 0004
- SmartCard.h lists the following error code
//
// MessageId: SCARD_E_INVALID_PARAMETER
//
// MessageText:
//
// One or more of the supplied parameters could not be properly
interpreted.
//
#define SCARD_E_INVALID_PARAMETER ((DWORD)0x80100004L)

Here's a quick stab at an answer:
After looking at your code, you may want to check your define of
SCardTransmit try changing the int you are marshalling for hCard to
something else. Similarly, that would require a change to your
SCardConnect.

My reasoning:
winscard.h defines the following:
typedef ULONG_PTR SCARDHANDLE;

Good Luck


Ramazzotti... said:
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);
 
Hello,

I have the same problem with the SCardTransmit Command. I'll try to
read/write a protected Memory-Card. Can you help me?

With best regards

Andreas
 
Back
Top