<div dir="ltr"><font class="fixed_width" face="Courier, Monospaced">Hello, <br> </font><p><font class="fixed_width" face="Courier, Monospaced">I'm trying out PLT Scheme FFI on WIn32. Examples included into the distribution show only simpler cases. <br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced">Here is the C snippet I'm trying to translate: <br> </font></p><span style="font-family: courier new,monospace;">typedef unsigned char BYTE;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">typedef BYTE* LPBYTE;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">typedef const BYTE* LPCBYTE;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">typedef unsigned long DWORD;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">typedef DWORD* LPDWORD;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">typedef void* SCARDHANDLE; // Just an handle.</span><br style="font-family: courier new,monospace;"><p style="font-family: courier new,monospace;"><font class="fixed_width">typedef struct _SCARD_IO_REQUEST{ <br>
DWORD dwProtocol; // Protocol identifier <br> DWORD cbPciLength; // Protocol Control Information Length <br> </font></p><div id="qhide_87640" style="display: block; font-family: courier new,monospace;" class="qt">
<font class="fixed_width">} SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; <br> <br></font></div><font style="font-family: courier new,monospace;" class="fixed_width" face="Courier, Monospaced">typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST; <br>
<br> </font><p><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">LONG SCardTransmit( </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> /* IN */ SCARDHANDLE hCard,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* IN */ </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">LPCSCARD_I0_REQUEST pioSendPci, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* IN */ </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">LPCBYTE pbSendBuffer, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* IN */ </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">DWORD cbSendLength, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* OUT */ </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">LPSCARD_IO_REQUEST pioRecvPci, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* OUT */</span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;"> LPBYTE pbRecvBuffer, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">/* OUT */ </span></font><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">LPDWORD pcbRecvLength </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ); </span><br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">Here the resulting PLT Scheme code, with compiler errors: <br> </font></p><p style="font-family: courier new,monospace;">
<font class="fixed_width">(module winscard mzscheme <br> (require mzlib/foreign) <br> (unsafe!) <br> </font></p><p style="font-family: courier new,monospace;"><font class="fixed_width"> (define _DWORD _int) ;; It should be 32 bits unsigned integer. <br>
(define _LONG _int) ;; OK: 32 bits integer. <br> (define _SCARDHANDLE _int) ;; This is an handle, I'd prefer using a <br> "typed" integer for type checking. <br> </font></p><p style="font-family: courier new,monospace;">
<font class="fixed_width"> (define-cstruct _SCARD_IO_REQUEST ((dwProtocol _DWORD) ;; // <br> Protocol identifier <br> (cbPciLength _DWORD))) ;; // <br> Protocol Control Information Length <br>
</font></p><p style="font-family: courier new,monospace;"><font class="fixed_width"> (define winscard (ffi-lib "winscard")) <br> </font></p><p style="font-family: courier new,monospace;"><font class="fixed_width"> ;; [simpler functions not included] <br>
</font></p><p style="font-family: courier new,monospace;"><font class="fixed_width"> (provide scard-transmit) <br> (define-struct scard-transmit-result (error recv-pci recv-buffer <br> recv-length)) <br> (define scard-transmit (get-ffi-obj "SCardTransmit" winscard <br>
(_fun (hCard : _SCARDHANDLE) <br> (pioSendPci : _SCARD_I0_REQUEST-pointer) ;; ERROR: expand: <br> unbound variable in module in: __SCARD_I0_REQUEST-pointer <br> (pbSendBuffer : (_cvector i _byte)) ;; ERROR: expand: <br>
unbound variable in module in: i <br> (cbSendLength : _DWORD) <br> (ioRecvPci : (_ptr o _SCARD_IO_REQUEST)) <br> (pbRecvBuffer : (_cvector o _byte )) ;; ERROR: expand: <br> unbound variable in module in: o <br>
(cbRecvLength : (_ptr o _DWORD)) <br> -> (error : _LONG) <br> -> (make-scard-transmit-result error ioRecvPci <br> pbRecvBuffer cbRecvLength)))) <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced"><span style="font-family: courier new,monospace;">) </span><br>
</font></p><p><font class="fixed_width" face="Courier, Monospaced">What's wrong? <br> </font></p><p><font class="fixed_width" face="Courier, Monospaced">And what about byte aligned, word aligned, double word aligned structures? What about calling conventions? <br>
</font></p><font class="fixed_width" face="Courier, Monospaced">Thanks </font></div>