Passing 64-bit int to DLL problem

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm

Passing 64-bit int to DLL problem

Post by m35 »

I'm having trouble with passing 64-bit integers to DLL functions. The DLL is compiled on Windows XP using MinGW, and used with newLISP v9.1.1.

Here is a test program that shows the problem:

test64.c

Code: Select all

#include <stdio.h>

__declspec (dllexport) int Test64(__int64 i)
{
	unsigned int * pi;
	pi = (unsigned int*)&i;
	printf("0x %08X %08X", pi[1], pi[0]);	
	return 0;
}
test64.def

Code: Select all

LIBRARY     TEST64.DLL

EXPORTS
	Test64 @1 Test64
make.bat

Code: Select all

C:\MinGW\bin\mingw32-gcc test64.c -c -Wall
C:\MinGW\bin\dllwrap.exe test64.o --enable-stdcall-fixup -def test64.def -o test64.dll
test64.lsp

Code: Select all

(import "test64.dll" "Test64")

(setq test-values 
   '(0x0000000087654321
     0x000000A987654321 
     0x0000CBA987654321))

(dolist (val test-values)

   (println (dump val))
   (println (format "0x %08X %08X"
      (get-int (+ 4 (address val)))
      (get-int (address val))
   ))
   (Test64 val)
   (println)(println)

)
(exit)
Output:

Code: Select all

(218736 898 212400 -2023406815 0)
0x 00000000 87654321
0x 000356D0 87654321

(218736 898 212400 -2023406815 169)
0x 000000A9 87654321
0x 000356F0 87654321

(218736 898 212400 -2023406815 52137)
0x 0000CBA9 87654321
0x 000356D0 87654321
32-bit integers pass without a problem, but I can't seem to get the most significant integer of the 64-bit number.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Only 32-bit values are handled. You have to pass the 64-bit value as two 32-bit entitities in the right sequence. On little-endian CPUs in your example:

Code: Select all

(Test64 low32 high32)
Lutz

ps: see also this thread with my longer explanations on Sun Jun 17, 2007:
http://www.alh.net/newlisp/phpbb/viewto ... =lp64#9412

ps2: you existing Test64 will take the low32 and high32 values from the stack (which is 32-bit wide), so no rewriting of Test64 is necessary.

m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm

Post by m35 »

Ah I see. Thank you Lutz.

Locked