Skip to content

Commit 5521627

Browse files
committed
Merge pull request #610 from lgoldstein/issue-604
Fixed issue #604: Kernel32#GetLastError() always returns ERROR_SUCCESS
2 parents 228177e + d83820e commit 5521627

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bug Fixes
5252
* [#578](https://github.com/java-native-access/jna/pull/578): Fix COM CallbackHandlers, allow usage of VARIANTs directly in c.s.j.p.w.COM.util.ProxyObject and fix native memory leak in c.s.j.p.w.COM.util.ProxyObject - [@matthiasblaesing](https://github.com/matthiasblaesing)
5353
* [#601](https://github.com/java-native-access/jna/pull/601): Remove COMThread and COM initialization from objects and require callers to initialize COM themselves. Asserts are added to guard correct usage. - [@matthiasblaesing](https://github.com/matthiasblaesing).
5454
* [#602] https://github.com/java-native-access/jna/pull/602): Make sure SID related memory is properly released once no longer required [@lgoldstein](https://github.com/lgoldstein).
55+
* [#610](https://github.com/java-native-access/jna/pull/610): Fixed issue #604: Kernel32#GetLastError() always returns ERROR_SUCCESS [@lgoldstein](https://github.com/lgoldstein).
5556

5657
Release 4.2.1
5758
=============

contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public static void main(String[] args) {
6161
junit.textui.TestRunner.run(Kernel32Test.class);
6262
}
6363

64+
// see https://github.com/java-native-access/jna/issues/604
65+
public void testGetLastErrorNativeLibraryOverride() {
66+
assertFalse("Unexpected success", Kernel32.INSTANCE.CloseHandle(null));
67+
assertEquals("Mismatched error code", WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
68+
}
69+
6470
// see https://github.com/twall/jna/issues/482
6571
public void testNoDuplicateMethodsNames() {
6672
Collection<String> dupSet = AbstractWin32TestSupport.detectDuplicateMethods(Kernel32.class);
@@ -295,11 +301,10 @@ public void testGetCurrentThreadId() {
295301

296302
public void testGetCurrentThread() {
297303
HANDLE h = Kernel32.INSTANCE.GetCurrentThread();
298-
assertNotNull(h);
299-
assertFalse(h.equals(0));
300-
// CloseHandle does not need to be called for a thread handle
301-
assertFalse(Kernel32.INSTANCE.CloseHandle(h));
302-
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
304+
assertNotNull("No current thread handle", h);
305+
assertFalse("Null current thread handle", h.equals(0));
306+
// Calling the CloseHandle function with this handle has no effect
307+
assertTrue(Kernel32.INSTANCE.CloseHandle(h));
303308
}
304309

305310
public void testOpenThread() {
@@ -316,11 +321,10 @@ public void testGetCurrentProcessId() {
316321

317322
public void testGetCurrentProcess() {
318323
HANDLE h = Kernel32.INSTANCE.GetCurrentProcess();
319-
assertNotNull(h);
320-
assertFalse(h.equals(0));
321-
// CloseHandle does not need to be called for a process handle
322-
assertFalse(Kernel32.INSTANCE.CloseHandle(h));
323-
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
324+
assertNotNull("No current process handle", h);
325+
assertFalse("Null current process handle", h.equals(0));
326+
// Calling the CloseHandle function with a pseudo handle has no effect
327+
assertTrue(Kernel32.INSTANCE.CloseHandle(h));
324328
}
325329

326330
public void testOpenProcess() {
@@ -332,8 +336,9 @@ public void testOpenProcess() {
332336
}
333337

334338
public void testQueryFullProcessImageName() {
335-
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, Kernel32.INSTANCE.GetCurrentProcessId());
336-
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process handle", h);
339+
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
340+
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, pid);
341+
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
337342

338343
try {
339344
char[] path = new char[WinDef.MAX_PATH];

src/com/sun/jna/NativeLibrary.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ private NativeLibrary(String libraryName, String libraryPath, long handle, Map<S
109109
synchronized(functions) {
110110
Function f = new Function(this, "GetLastError", Function.ALT_CONVENTION, encoding) {
111111
@Override
112-
Object invoke(Object[] args, Class<?> returnType, boolean b) {
112+
Object invoke(Object[] args, Class<?> returnType, boolean b, int fixedArgs) {
113+
return Integer.valueOf(Native.getLastError());
114+
}
115+
116+
@Override
117+
Object invoke(Method invokingMethod, Class<?>[] paramTypes, Class<?> returnType, Object[] inArgs, Map<String, ?> options) {
113118
return Integer.valueOf(Native.getLastError());
114119
}
115120
};

0 commit comments

Comments
 (0)