Skip to content

Commit

Permalink
Fixed issue #604: Kernel32#GetLastError() always returns ERROR_SUCCESS
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyor Goldstein committed Mar 5, 2016
1 parent 228177e commit d83820e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Bug Fixes
* [#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)
* [#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).
* [#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).
* [#610](https://github.com/java-native-access/jna/pull/610): Fixed issue #604: Kernel32#GetLastError() always returns ERROR_SUCCESS [@lgoldstein](https://github.com/lgoldstein).

Release 4.2.1
=============
Expand Down
29 changes: 17 additions & 12 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public static void main(String[] args) {
junit.textui.TestRunner.run(Kernel32Test.class);
}

// see https://github.com/java-native-access/jna/issues/604
public void testGetLastErrorNativeLibraryOverride() {
assertFalse("Unexpected success", Kernel32.INSTANCE.CloseHandle(null));
assertEquals("Mismatched error code", WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}

// see https://github.com/twall/jna/issues/482
public void testNoDuplicateMethodsNames() {
Collection<String> dupSet = AbstractWin32TestSupport.detectDuplicateMethods(Kernel32.class);
Expand Down Expand Up @@ -295,11 +301,10 @@ public void testGetCurrentThreadId() {

public void testGetCurrentThread() {
HANDLE h = Kernel32.INSTANCE.GetCurrentThread();
assertNotNull(h);
assertFalse(h.equals(0));
// CloseHandle does not need to be called for a thread handle
assertFalse(Kernel32.INSTANCE.CloseHandle(h));
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
assertNotNull("No current thread handle", h);
assertFalse("Null current thread handle", h.equals(0));
// Calling the CloseHandle function with this handle has no effect
assertTrue(Kernel32.INSTANCE.CloseHandle(h));
}

public void testOpenThread() {
Expand All @@ -316,11 +321,10 @@ public void testGetCurrentProcessId() {

public void testGetCurrentProcess() {
HANDLE h = Kernel32.INSTANCE.GetCurrentProcess();
assertNotNull(h);
assertFalse(h.equals(0));
// CloseHandle does not need to be called for a process handle
assertFalse(Kernel32.INSTANCE.CloseHandle(h));
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
assertNotNull("No current process handle", h);
assertFalse("Null current process handle", h.equals(0));
// Calling the CloseHandle function with a pseudo handle has no effect
assertTrue(Kernel32.INSTANCE.CloseHandle(h));
}

public void testOpenProcess() {
Expand All @@ -332,8 +336,9 @@ public void testOpenProcess() {
}

public void testQueryFullProcessImageName() {
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, Kernel32.INSTANCE.GetCurrentProcessId());
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process handle", h);
int pid = Kernel32.INSTANCE.GetCurrentProcessId();
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, pid);
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);

try {
char[] path = new char[WinDef.MAX_PATH];
Expand Down
7 changes: 6 additions & 1 deletion src/com/sun/jna/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ private NativeLibrary(String libraryName, String libraryPath, long handle, Map<S
synchronized(functions) {
Function f = new Function(this, "GetLastError", Function.ALT_CONVENTION, encoding) {
@Override
Object invoke(Object[] args, Class<?> returnType, boolean b) {
Object invoke(Object[] args, Class<?> returnType, boolean b, int fixedArgs) {
return Integer.valueOf(Native.getLastError());
}

@Override
Object invoke(Method invokingMethod, Class<?>[] paramTypes, Class<?> returnType, Object[] inArgs, Map<String, ?> options) {
return Integer.valueOf(Native.getLastError());
}
};
Expand Down

0 comments on commit d83820e

Please sign in to comment.