0
Reply

P/Invoke on Compact Framework with Complex Structures

Andrew Bailie

Andrew Bailie

Dec 18 2009 7:22 AM
4.9k
Hi all,

As a bit of background, I'm developing (hardware and software) a Windows CE (6.0) based device, which includes a smartcard reader that I've written a PC/SC driver for.

The customer wants the user interface to be written in c#, which I'm by no means a master of,  so I'm trying to access the API from with in the .NetCF. The problem I've come up against is with the SCardGetStatusChange function which has the following prototype:

LONG SCardGetStatusChange(
  SCARDCONTEXT hContext, 
  DWORD dwTimeout, 
  LPSCARD_READERSTATE rgReaderStates, 
  DWORD cReaders 
);

where LPSCARD_READERSTATE points to an array of SCARD_READERSTATE structs:

typedef struct {
  LPCTSTR szReader;
  LPVOID pvUserData;
  DWORD dwCurrentState;
  DWORD dwEventState;
  DWORD cbAtr;
  BYTE rgbAtr[36];
} SCARD_READERSTATE, *PSCARD_READERSTATE, *LPSCARD_READERSTATE;

I've been searching the web for help on this and all the things I've tried have failed in some form or other. I have got it working in the desktop environment, but that's because complex structs are handle automatically(ish). szReader is known, because it is the name of the reader to be monitored and you pass this to the function.

For the desktop environment the main parts of the code are as follows:

[DllImport("winscard.dll", SetLastError = true)]
internal static extern int SCardGetStatusChange(UInt32 hContext,
            UInt32 dwTimeout,
            [In,Out] SCard_ReaderState[] rgReaderStates,
            UInt32 cReaders);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SCard_ReaderState
{
        public string szReader;
        public IntPtr pvUserData;
        public UInt32 dwCurrentState;
        public UInt32 dwEventState;
        public UInt32 cbAtr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=36)]
        public byte[] rgbAtr;
}

SCardGetStatusChange(hContext, WAIT_TIME, readerState, nbReaders);

I'd be grateful if someone could help me out here or point me in the right direction.

Thanks,

Andrew