Thank you for the help and for the additional information about this issue!<div><br></div><div>Regards,</div><div>Roman<br><br><div class="gmail_quote">2010/10/6 Gary Byers <span dir="ltr"><<a href="mailto:gb@clozure.com">gb@clozure.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">This sounds like one of several bugs in the win64 FFI code that was fixed<br>
in the trunk in <<a href="http://trac.clozure.com/ccl/changeset/14156" target="_blank">http://trac.clozure.com/ccl/changeset/14156</a>>. That change<br>
wasn't propagated to the 1.5 sources, though I don't think that there's any<br>
reason that it couldn't be.<br>
<br>
Win64's calling conventions differ from those used on other x8664 platforms;<br>
the differences have to do with how many arguments are passed in registers<br>
and how floating-point and non-fp args are distributed between floating-point<br>
and general-purpose registers. For whatever reason, DEFCALLBACK on win64<br>
seems to have gotten these details right, but FF-CALL was basically trying<br>
to use the same conventions that other platforms use.<br>
<br>
The change in r14156 does (as far as I can tell) fix your test case; I applied<br>
the same changes to 1.5, verified that they seem to work, and committed them<br>
to the 1.5 branch (as r14332.)<br>
<br>
As far as I can tell, there's nothing wrong with your code; the code that<br>
implements FF-CALL just wasn't putting the function's arguments in the right<br>
places.<div><div></div><div class="h5"><br>
<br>
On Wed, 6 Oct 2010, Roman Marynchak wrote:<br>
<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">
Hello,<br>
?I have a strange issue with a simple FFI example for CCL 1.5 ("Version<br>
1.5-r13651 ?(WindowsX8664)") running on Win7 x64.<br>
<br>
There is a dynamic library which exports this function:<br>
<br>
extern "C" {<br>
SAMPLELIB_API int increment_array(double flag, unsigned short* array)<br>
{<br>
if (flag > 6.3) {<br>
array[0] = 83;<br>
array[1] += 2;<br>
array[2] += 7;<br>
array[3] = 61372;<br>
return 1;<br>
} else {<br>
array[0] = 726;<br>
array[1] += 111;<br>
array[2] += 222;<br>
array[3] = 888;<br>
return 2;<br>
}<br>
}<br>
}<br>
<br>
<br>
And the code below tries to invoke the foreign function:<br>
<br>
(defun allocate-sample-array (required-length initial-value)<br>
??(declare (type fixnum required-length))<br>
??(declare (type (unsigned-byte 16) initial-value))<br>
??(multiple-value-bind (array pointer)<br>
?? ? ?(make-heap-ivector required-length '(unsigned-byte 16))<br>
?? ?(loop for i from 0 below required-length do<br>
(setf (aref array i) initial-value))<br>
?? ?(list array pointer)))<br>
<br>
(defun do-test ()<br>
??(let* ((lib (open-shared-library "D:\\SampleLib.dll"))<br>
(array-desc-pair (allocate-sample-array 4 1))<br>
(array (first array-desc-pair))<br>
(ptr (second array-desc-pair)))<br>
?? ?(external-call "increment_array" :double-float 1.5d+2 :address ptr<br>
:integer)<br>
?? ?(loop for element across array do (print element))<br>
?? ?#|(close-shared-library lib)|#))<br>
<br>
<br>
DO_TEST crashes in EXTERNAL-CALL. Debugging with VS shows that<br>
increment_array is invoked, but the second argument is a null pointer. I<br></div></div>
guess that I have some issue in my code, but Ifollowed?<a href="http://openmcl.clozure.com/manual/chapter12.11.html#Tutorial--Allo" target="_blank">http://openmcl.clozure.com/manual/chapter12.11.html#Tutorial--Allo</a><div class="im">
<br>
cating-Foreign-Data-on-the-Lisp-Heap?while writing it. Could somebody please<br>
help me to understand what is wrong?<br>
<br>
Also, CLOSE-SHARED-LIBRARY function seems to be absent in this CCL image. Is<br>
there any replacement for it?<br>
<br>
<br>
Thank you,<br></div>
Roman Marynchak?<br>
<br>
<br>
</blockquote>
</blockquote></div><br></div>