Skip to content

Commit

Permalink
Fixed issue #604: Kernel#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 9b1e3cb
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 @@ -68,6 +68,7 @@ Features
Bug Fixes
---------
* [#515](https://github.com/java-native-access/jna/issues/515): Update linux-arm natives omitted in 4.2 [@twall](https://github.com/twall).
* [#604](https://github.com/java-native-access/jna/issues/604): Kernel#GetLastError() always returns ERROR_SUCCESS [@lgoldstein](https://github.com/lgoldstein).

Release 4.2
===========
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 9b1e3cb

Please sign in to comment.