pinvoke problem

T

Thomas Mobley

I'm having a problem getting the name of my card readers using
winscard. The same function works in vb6, but I think I must be
missing something here. Anybody see what I'm doing wrong?

Following are my calls to the imports and my values (only the relevent
stuff)
SCardEstablishContext works properly and returns the proper handle
(checks in my vb app)

SCardListReadersW returns success in result, and pReaders get the
correct length (verifies in vb app), but Readers is emptied (even the
null is gone)
++


StringBuilder Readers = new StringBuilder(2048);
Readers.Append("\0");
UInt32 pReaders = 0;
IntPtr pContext = IntPtr.Zero;
int SCardContext = 0;

SCARD_READERSTATEA ReaderState = new SCARD_READERSTATEA(0);
Result = SCardEstablishContext(SCARD_SCOPE_SYSTEM, 0, 0, ref
pContext);
if (Result != 0)
MessageBox.Show(GetErrorMessage(Result));
Result = SCardListReadersW(pContext, null, Readers, ref pReaders);

[DllImport("winscard.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int SCardEstablishContext(int dwScope, int
pvReserved1, int pvReserved2, ref IntPtr phContext);

[DllImport("winscard.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int SCardListReadersW(IntPtr hContext, String
mszGroups, StringBuilder mszReaders, ref UInt32 pcchReaders);


The calls in the winscard lib file are as follows:

Extern WINSCARDAPI LONG WINAPI
SCardListReadersW(
IN SCARDCONTEXT hContext,
IN LPCWSTR mszGroups,
OUT LPWSTR mszReaders,
IN OUT LPDWORD pcchReaders);

extern WINSCARDAPI LONG WINAPI
SCardEstablishContext(
IN DWORD dwScope,
IN LPCVOID pvReserved1,
IN LPCVOID pvReserved2,
OUT LPSCARDCONTEXT phContext);
 
M

Mattias Sjögren

Thomas,
UInt32 pReaders = 0;

pReaders should be the length of the Readers buffer on input. Since
you set it to zero, nothing will get copied to the buffer. You
probably want this instead

UInt32 pReaders = (UInt32)Readers.Capacity;

[DllImport("winscard.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int SCardListReadersW(IntPtr hContext, String
mszGroups, StringBuilder mszReaders, ref UInt32 pcchReaders);

You shouldn't combine CharSet.Auto with explicitly specifying the W
version of the API. Either remove the W and let the runtime pick the
right version, A or W, or keep the explicit W but set CharSet to
Unicode.



Mattias
 
T

Thomas Mobley

That did the trick. Greatly appreciated.

Thomas,
UInt32 pReaders = 0;

pReaders should be the length of the Readers buffer on input. Since
you set it to zero, nothing will get copied to the buffer. You
probably want this instead

UInt32 pReaders = (UInt32)Readers.Capacity;

[DllImport("winscard.dll",
CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int SCardListReadersW(IntPtr hContext, String
mszGroups, StringBuilder mszReaders, ref UInt32 pcchReaders);

You shouldn't combine CharSet.Auto with explicitly specifying the W
version of the API. Either remove the W and let the runtime pick the
right version, A or W, or keep the explicit W but set CharSet to
Unicode.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top