{"record":{"id":"2caeb3fc1ab580cc","repo":"java-native-access/jna","slug":"win32exception-kernel32-instance-getlasterror-psapiutil","errorCode":null,"errorMessage":"Win32Exception(Kernel32.INSTANCE.GetLastError())","messagePattern":"Win32Exception\\(Kernel32\\.INSTANCE\\.GetLastError\\(\\)\\)","errorType":"error_code","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/PsapiUtil.java","lineNumber":53,"sourceCode":" *\n * @author Torbj&ouml;rn Svensson, azoff[at]svenskalinuxforeningen.se\n */\npublic abstract class PsapiUtil {\n\n    /**\n     * Retrieves the process identifier for each process object in the system.\n     *\n     * @return Array of pids\n     */\n    public static int[] enumProcesses() {\n        int size = 0;\n        int[] lpidProcess = null;\n        IntByReference lpcbNeeded = new IntByReference();\n        do {\n            size += 1024;\n            lpidProcess = new int[size];\n            if (!Psapi.INSTANCE.EnumProcesses(lpidProcess, size * DWORD.SIZE, lpcbNeeded)) {\n                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n            }\n        } while (size == lpcbNeeded.getValue() / DWORD.SIZE);\n\n        return Arrays.copyOf(lpidProcess, lpcbNeeded.getValue() / DWORD.SIZE);\n    }\n\n    /**\n     * Retrieves the name of the executable file for the specified process.\n     *\n     * @param hProcess\n     *            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","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/PsapiUtil.java#L35-L71","documentation":"PsapiUtil.enumProcesses lists process IDs with PSAPI EnumProcesses, growing the buffer until it holds all PIDs. When EnumProcesses returns FALSE the library throws Win32Exception built from Kernel32 GetLastError, carrying the Win32 error code and message.","triggerScenarios":"EnumProcesses failing with FALSE — e.g. ERROR_ACCESS_DENIED in restricted security contexts, or a native/PSAPI failure while sizing the buffer in the growth loop (size grows by 1024 entries per iteration).","commonSituations":"Running inside restricted service accounts or hardened environments where process enumeration is blocked; sandboxed/containerized Windows processes with limited API access; debugging hooks (antivirus) interfering with PSAPI calls.","solutions":["Read the Win32 error code from Win32Exception; ERROR_ACCESS_DENIED means run with sufficient privileges","Run the application under an account permitted to enumerate processes (elevated or a standard service account)","Catch Win32Exception and degrade gracefully (e.g. return an empty/known list) if enumeration is non-essential","Verify no security software injects/fails PSAPI calls; test on a clean host"],"exampleFix":"// before\nint[] pids = PsapiUtil.enumProcesses();\n// after\nint[] pids;\ntry {\n    pids = PsapiUtil.enumProcesses();\n} catch (Win32Exception e) {\n    LOG.warn(\"enumProcesses failed: \" + e.getErrorCode());\n    pids = new int[0];\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return PsapiUtil.enumProcesses(); } catch (Win32Exception e) { LOG.warn(\"EnumProcesses failed: \" + e.getErrorCode()); return new int[0]; }","preventionTips":["Run under an account permitted to enumerate processes","Expect failure in hardened/containerized Windows environments and degrade gracefully","Check the wrapped Win32 error code to distinguish access denied from transient failures"],"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"}