From 21a5df9e7cf66f86b75dd0373a356a3251ad01e7 Mon Sep 17 00:00:00 2001 From: Timothy Wall Date: Sun, 6 Dec 2015 10:20:08 -0500 Subject: [PATCH] Add test for small stdcall arguments --- native/testlib.c | 5 +++++ src/com/sun/jna/win32/StdCallFunctionMapper.java | 10 ++++++++-- test/com/sun/jna/win32/W32StdCallTest.java | 9 +++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/native/testlib.c b/native/testlib.c index 7080d1d76d..11f8a2bee5 100644 --- a/native/testlib.c +++ b/native/testlib.c @@ -925,6 +925,11 @@ returnStructureByValueArgumentStdCall(TestStructureByValue arg) { return arg; } +EXPORT int16_t __stdcall +returnInt16ArgumentStdCall(int16_t arg1, int16_t arg2, void* buffer, int16_t len) { + return len; +} + EXPORT int32_t __stdcall callInt32StdCallCallback(int32_t (__stdcall *func)(int32_t arg, int32_t arg2), int32_t arg, int32_t arg2) { diff --git a/src/com/sun/jna/win32/StdCallFunctionMapper.java b/src/com/sun/jna/win32/StdCallFunctionMapper.java index 1a78c578fd..5e2913d40e 100644 --- a/src/com/sun/jna/win32/StdCallFunctionMapper.java +++ b/src/com/sun/jna/win32/StdCallFunctionMapper.java @@ -40,7 +40,7 @@ protected int getArgumentNativeStackSize(Class cls) { return Pointer.SIZE; } try { - return Native.getNativeSize(cls); + return Math.max(4, Native.getNativeSize(cls)); } catch(IllegalArgumentException e) { throw new IllegalArgumentException("Unknown native stack allocation size for " + cls); @@ -59,12 +59,18 @@ public String getFunctionName(NativeLibrary library, Method method) { String decorated = name + "@" + pop; int conv = StdCallLibrary.STDCALL_CONVENTION; try { + if (Native.DEBUG_LOAD) { + System.out.println("Trying " + decorated); + } name = library.getFunction(decorated, conv).getName(); } catch(UnsatisfiedLinkError e) { // try with an explicit underscore try { + if (Native.DEBUG_LOAD) { + System.out.println("Trying _" + decorated); + } name = library.getFunction("_" + decorated, conv).getName(); } catch(UnsatisfiedLinkError e2) { @@ -73,4 +79,4 @@ public String getFunctionName(NativeLibrary library, Method method) { } return name; } -} \ No newline at end of file +} diff --git a/test/com/sun/jna/win32/W32StdCallTest.java b/test/com/sun/jna/win32/W32StdCallTest.java index 5404c32a81..5bce3a0ee5 100644 --- a/test/com/sun/jna/win32/W32StdCallTest.java +++ b/test/com/sun/jna/win32/W32StdCallTest.java @@ -24,6 +24,7 @@ import com.sun.jna.Native; import com.sun.jna.NativeLibrary; import com.sun.jna.NativeLong; +import com.sun.jna.Pointer; import com.sun.jna.Structure; /** @@ -50,6 +51,7 @@ protected List getFieldOrder() { } } int returnInt32ArgumentStdCall(int arg); + short returnInt16ArgumentStdCall(short arg1, short arg2, Pointer buf, short len); TestStructure.ByValue returnStructureByValueArgumentStdCall(TestStructure.ByValue arg); interface Int32Callback extends StdCallCallback { int callback(int arg, int arg2); @@ -125,6 +127,13 @@ public void testStdCallReturnStructureByValueArgument() { assertTrue("Wrong struct value", s.dataEquals(testlib.returnStructureByValueArgumentStdCall(s))); } + public void testStdCallStackAlignment() { + final short MAGIC = 0x1234; + assertEquals("Incorrect stdcall stack alignment", MAGIC, + testlib.returnInt16ArgumentStdCall((short)1, (short)2, + null, MAGIC)); + } + public void testStdCallCallback() { final int MAGIC = 0x11111111; final boolean[] called = { false };