If you saw that early post, I was kind of wrestling with this, I
figured it out, it is kind of interesting if you want a strange
interface to C(or maybe not interesting). I kind of documented it for
all to see.
;;;
;;;
;;; Date: 12/14/2004
;;;
;;; Env:
;;;
;;; Win32 - GNU CLISP
;;;
;;; Here is an example of defining a struct in LISP,
;;; one of the members contains a pointer to a function,
;;; the function is defined in lisp. I create an instance
;;; of the struct in lisp locally with lisp. Now I want to
;;; pass that instance of the struct to a C/DLL and call
;;; the function that was defined in LISP.
;;;
;;; (I come from the object world,
;;; excuse my use of terms like instance when talking about C and Lisp)
;;;
;;; also see:
;;;
;;; http://clisp.cons.org/impnotes/dffi.html
;;;
;;; PSEUDO code and working code
;;;
;;;
;;; With FFI create the foreign C function
;;;
(def-call-out widget-testpointer-ptr
(:name "TestWNDCLASS")
(:arguments (lisp-wndclass-ptr c-pointer))
(:return-type)
(:library "WidgetToolkit.dll"))
;;;
;;; Create a new variable binding of the simple C struct
;;;
(let ((simpl (make-test-simpl :cbSize 32
:lpfnWndProc #'test-wndproc)))
(with-c-var (bb 'test-simpl simpl)
(widget-testpointer-ptr (c-var-address bb))))
;;;
;;; Define the function that we will later use in our
;;; pointer to function struct.
;;;
(defun test-wndproc (hwnd message wparam lparam)
(declare (type ffi:c-pointer hwnd))
(declare (type ffi:uint message))
(declare (type ffi:long lparam))
;;; return ;;;
(let ((lresult -1))
(declare (type long lresult))
(print "Hello Function Pointer")
lresult))
#|
------------------------------------------
typedef long(CALLBACK *LISP_WNDPROC)(void*,unsigned int,unsigned
int,long);
typedef struct __TEST_WND {
unsigned int style;
LISP_WNDPROC lpfnWndProc;
} TEST_WND;
///
/// With this C code
///
/// (print "Hello Function Pointer")
///
/// This is output to the console before
/// the MessageBox comes up.
///
DLLIMPORT void TestWNDCLASS(void *ptr) {
TEST_WND *local = ptr;
char buf[80];
sprintf(buf, "002: Test Get LISP Pointer - 0x%x : 0x%d",
local, local->style);
local->lpfnWndProc(NULL, 22, 33, 40);
MessageBox(0, buf, "Test!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
} // end of the function //
**The Final Output **:
First the text "Hello Function Pointer" is output to the console
A messagebox pops up with the pointer to that struct in LISP
------------------------------------------
|#
1