[plt-scheme] PLT Scheme FFI on Win32: handling structures and byte arrays

From: Elena Garrulo (egarrulo at gmail.com)
Date: Wed Aug 6 04:37:15 EDT 2008

Hello,

I'm trying out PLT Scheme FFI on WIn32. Examples included into the
distribution show only simpler cases.

Here is the C snippet I'm trying to translate:
typedef unsigned char BYTE;
typedef BYTE* LPBYTE;
typedef const BYTE* LPCBYTE;
typedef unsigned long DWORD;
typedef DWORD* LPDWORD;
typedef void* SCARDHANDLE; // Just an handle.

typedef struct _SCARD_IO_REQUEST{
    DWORD dwProtocol;   // Protocol identifier
    DWORD cbPciLength;  // Protocol Control Information Length
} SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST;

typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST;

LONG SCardTransmit(
  /* IN */  SCARDHANDLE hCard,
  /* IN */  LPCSCARD_I0_REQUEST pioSendPci,
  /* IN */  LPCBYTE pbSendBuffer,
  /* IN */  DWORD cbSendLength,
  /* OUT */ LPSCARD_IO_REQUEST pioRecvPci,
  /* OUT */ LPBYTE pbRecvBuffer,
  /* OUT */ LPDWORD pcbRecvLength
);

Here the resulting PLT Scheme code, with compiler errors:

(module winscard mzscheme
  (require mzlib/foreign)
  (unsafe!)

  (define _DWORD _int) ;; It should be 32 bits unsigned integer.
  (define _LONG _int) ;; OK: 32 bits integer.
  (define _SCARDHANDLE _int) ;; This is an handle, I'd prefer using a
"typed" integer for type checking.

  (define-cstruct _SCARD_IO_REQUEST ((dwProtocol _DWORD) ;;   //
Protocol identifier
                                     (cbPciLength _DWORD))) ;;  //
Protocol Control Information Length

  (define winscard (ffi-lib "winscard"))

   ;; [simpler functions not included]

  (provide scard-transmit)
  (define-struct scard-transmit-result (error recv-pci recv-buffer
recv-length))
  (define scard-transmit (get-ffi-obj "SCardTransmit" winscard
      (_fun (hCard : _SCARDHANDLE)
            (pioSendPci : _SCARD_I0_REQUEST-pointer) ;; ERROR: expand:
unbound variable in module in: __SCARD_I0_REQUEST-pointer
            (pbSendBuffer : (_cvector i _byte)) ;; ERROR: expand:
unbound variable in module in: i
            (cbSendLength : _DWORD)
            (ioRecvPci : (_ptr o _SCARD_IO_REQUEST))
            (pbRecvBuffer :  (_cvector o _byte )) ;; ERROR: expand:
unbound variable in module in: o
            (cbRecvLength : (_ptr o _DWORD))
            -> (error : _LONG)
            -> (make-scard-transmit-result error ioRecvPci
pbRecvBuffer cbRecvLength))))

)

What's wrong?

And what about byte aligned, word aligned, double word aligned  structures?
What about calling conventions?
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080806/d152f45b/attachment.html>

Posted on the users mailing list.