{"record":{"id":"5852038304898759","repo":"java-native-access/jna","slug":"win32exception-native-getlasterror-psapiutil","errorCode":null,"errorMessage":"Win32Exception(Native.getLastError())","messagePattern":"Win32Exception\\(Native\\.getLastError\\(\\)\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/PsapiUtil.java","lineNumber":82,"sourceCode":"     *            A handle to the process. The handle must have the\n     *            PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION\n     *            access right. For more information, see Process Security and\n     *            Access Rights. <br>\n     *            Windows Server 2003 and Windows XP: The handle must have the\n     *            PROCESS_QUERY_INFORMATION access right.\n     * @return ame of the executable file for the specified process.\n     * @throws Win32Exception in case an error occurs\n     * @see <a href=\"http://msdn.microsoft.com/en-us/library/ms683217(VS.85).aspx\">MSDN</a>\n     */\n    public static String GetProcessImageFileName(HANDLE hProcess) {\n        int size = 2048;\n        while (true) {\n            final char[] filePath = new char[size];\n            int length = Psapi.INSTANCE.GetProcessImageFileName(hProcess,\n                filePath, filePath.length);\n            if(length == 0) {\n                if(Native.getLastError() != WinError.ERROR_INSUFFICIENT_BUFFER) {\n                    throw new Win32Exception(Native.getLastError());\n                }\n                size += 2048;\n            } else {\n                return Native.toString(filePath);\n            }\n        }\n    }\n}\n","sourceCodeStart":64,"sourceCodeEnd":91,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/PsapiUtil.java#L64-L91","documentation":"PsapiUtil.GetProcessImageFileName resolves a process handle to its image path using PSAPI GetProcessImageFileName in a growing-buffer loop. If the call returns length 0 and GetLastError is anything other than ERROR_INSUFFICIENT_BUFFER, the library throws Win32Exception wrapping the native error code.","triggerScenarios":"Passing an invalid or closed process handle (ERROR_INVALID_HANDLE), lacking PROCESS_QUERY_INFORMATION rights on the target process (ERROR_ACCESS_DENIED — e.g. protected/system processes), or the process exiting mid-call.","commonSituations":"Inspecting another user's or a protected process (antivirus, services); holding a handle to a process that has since terminated; opening handles with insufficient access masks (OpenProcess without PROCESS_QUERY_LIMITED_INFORMATION).","solutions":["Verify the HANDLE is valid and the process is still running before calling","Open the process with PROCESS_QUERY_LIMITED_INFORMATION instead of full access to reduce ERROR_ACCESS_DENIED","Read the error code from Win32Exception: ERROR_INVALID_HANDLE -> fix handle lifecycle; ERROR_ACCESS_DENIED -> permissions issue","Catch Win32Exception and fall back to another lookup method (e.g. Kernel32 QueryFullProcessImageName via JNA) if unavailable"],"exampleFix":"// before\nHANDLE h = Kernel32.INSTANCE.OpenProcess(WinBase.PROCESS_ALL_ACCESS, false, pid);\nString path = PsapiUtil.GetProcessImageFileName(h);\n// after\nHANDLE h = Kernel32.INSTANCE.OpenProcess(WinBase.PROCESS_QUERY_LIMITED_INFORMATION, false, pid);\nString path = h == null ? null : PsapiUtil.GetProcessImageFileName(h);","handlingStrategy":"try-catch","validationCode":"if (hProcess == null) throw new IllegalArgumentException(\"process handle required\");","typeGuard":"boolean isOpenHandle(HANDLE h) { return h != null && !WinBase.INVALID_HANDLE_VALUE.equals(h); }","tryCatchPattern":"try { return PsapiUtil.GetProcessImageFileName(h); } catch (Win32Exception e) { if (e.getErrorCode() == WinError.ERROR_ACCESS_DENIED) return null; throw e; }","preventionTips":["Open processes with PROCESS_QUERY_LIMITED_INFORMATION to minimize access-denied errors","Check the process is alive and the handle valid before querying","Anticipate protected/system processes being unqueryable"],"tags":["windows","psapi","processes","jni"],"backgroundTag":"permission-denied","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}