[Openmcl-devel] Problem with FFI on Win64

Roman Marynchak roman.marynchak at gmail.com
Wed Oct 6 01:35:06 PDT 2010


Hello,

 I have a strange issue with a simple FFI example for CCL 1.5 ("Version
1.5-r13651  (WindowsX8664)") running on Win7 x64.

There is a dynamic library which exports this function:

extern "C" {
SAMPLELIB_API int increment_array(double flag, unsigned short* array)
{
if (flag > 6.3) {
array[0] = 83;
array[1] += 2;
array[2] += 7;
array[3] = 61372;
return 1;
} else {
array[0] = 726;
array[1] += 111;
array[2] += 222;
array[3] = 888;
return 2;
}
}
}


And the code below tries to invoke the foreign function:

(defun allocate-sample-array (required-length initial-value)
  (declare (type fixnum required-length))
  (declare (type (unsigned-byte 16) initial-value))
  (multiple-value-bind (array pointer)
      (make-heap-ivector required-length '(unsigned-byte 16))
    (loop for i from 0 below required-length do
 (setf (aref array i) initial-value))
    (list array pointer)))

(defun do-test ()
  (let* ((lib (open-shared-library "D:\\SampleLib.dll"))
 (array-desc-pair (allocate-sample-array 4 1))
 (array (first array-desc-pair))
 (ptr (second array-desc-pair)))
    (external-call "increment_array" :double-float 1.5d+2 :address ptr
:integer)
    (loop for element across array do (print element))
    #|(close-shared-library lib)|#))


DO_TEST crashes in EXTERNAL-CALL. Debugging with VS shows that
increment_array is invoked, but the second argument is a null pointer. I
guess that I have some issue in my code, but I followed
http://openmcl.clozure.com/manual/chapter12.11.html#Tutorial--Allocating-Foreign-Data-on-the-Lisp-Heap
while
writing it. Could somebody please help me to understand what is wrong?

Also, CLOSE-SHARED-LIBRARY function seems to be absent in this CCL image. Is
there any replacement for it?


Thank you,
Roman Marynchak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20101006/8571b8d2/attachment.htm>


More information about the Openmcl-devel mailing list