diff --git a/CHANGES.md b/CHANGES.md index 34bdcca29..40b1b09f5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ Features Bug Fixes --------- +* [#1700](https://github.com/java-native-access/jna/issues/1700): Windows: treat `ERROR_INVALID_PARAMETER` from `OpenProcess` in `c.s.j.p.WindowUtils#getProcessFilePath` like `ERROR_ACCESS_DENIED` to avoid spurious `Win32Exception` when enumerating windows whose processes have exited or are otherwise inaccessible - [marktech0813](https://github.com/marktech0813). Release 5.18.1 @@ -1465,4 +1466,4 @@ Bug Fixes * Ensure native libraries are only loaded once until released * Properly handle NULL when the return value is a Structure * Proper conversion to wchar_t on linux -* Copy full length of Java strings to C strings instead of stopping when a NUL character is encountered \ No newline at end of file +* Copy full length of Java strings to C strings instead of stopping when a NUL character is encountered diff --git a/contrib/platform/src/com/sun/jna/platform/WindowUtils.java b/contrib/platform/src/com/sun/jna/platform/WindowUtils.java index 78ea1fa2d..9c4c24e3e 100644 --- a/contrib/platform/src/com/sun/jna/platform/WindowUtils.java +++ b/contrib/platform/src/com/sun/jna/platform/WindowUtils.java @@ -1298,22 +1298,25 @@ public String getProcessFilePath(final HWND hwnd) { pid.getValue()); if (process == null) { - if(Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_ACCESS_DENIED) { - throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); - } else { - process = Kernel32.INSTANCE.OpenProcess( - WinNT.PROCESS_QUERY_LIMITED_INFORMATION, - false, - pid.getValue()); - - if (process == null) { - if (Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_ACCESS_DENIED) { - throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); - } else { - // Ignore windows, that can't be accessed - return ""; + switch (Kernel32.INSTANCE.GetLastError()) { + case WinNT.ERROR_ACCESS_DENIED: + case WinError.ERROR_INVALID_PARAMETER: + process = Kernel32.INSTANCE.OpenProcess( + WinNT.PROCESS_QUERY_LIMITED_INFORMATION, + false, + pid.getValue()); + if (process != null) { + break; } - } + switch (Kernel32.INSTANCE.GetLastError()) { + case WinNT.ERROR_ACCESS_DENIED: + case WinError.ERROR_INVALID_PARAMETER: + // Ignore windows, that can't be accessed + return ""; + } + /* if above didn't already break or return, fall through to default */ + default: + throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } }