{"record":{"id":"d008ef89e6b6f75c","repo":"java-native-access/jna","slug":"pdhexception-result","errorCode":null,"errorMessage":"PdhException(result)","messagePattern":"PdhException\\(result\\)","errorType":"error_code","errorClass":"PdhException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/PdhUtil.java","lineNumber":71,"sourceCode":"     *            Null-terminated string that specifies the name of the computer\n     *            where the specified performance object or counter is located.\n     *            The computer name can be specified by the DNS name or the IP\n     *            address. If NULL, the function uses the local computer.\n     * @param dwNameIndex\n     *            Index of the performance object or counter.\n     * @return Returns the name of the performance object or counter.\n     */\n    public static String PdhLookupPerfNameByIndex(String szMachineName, int dwNameIndex) {\n        // Call once with null buffer to get required buffer size\n        DWORDByReference pcchNameBufferSize = new DWORDByReference(new DWORD(0));\n        int result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, null, pcchNameBufferSize);\n        Memory mem = null;\n        // Windows XP requires a non-null buffer and nonzero buffer size and\n        // will return PDH_INVALID_ARGUMENT.\n        if (result != PdhMsg.PDH_INVALID_ARGUMENT) {\n            // Vista+ branch: use returned buffer size for second query\n            if (result != WinError.ERROR_SUCCESS && result != Pdh.PDH_MORE_DATA) {\n                throw new PdhException(result);\n            }\n            // Can't allocate 0 memory\n            if (pcchNameBufferSize.getValue().intValue() < 1) {\n                return \"\";\n            }\n            // Allocate buffer and call again\n            mem = new Memory(pcchNameBufferSize.getValue().intValue() * CHAR_TO_BYTES);\n            result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, mem, pcchNameBufferSize);\n        } else {\n            // XP branch: try increasing buffer sizes until successful\n            for (int bufferSize = 32; bufferSize <= Pdh.PDH_MAX_COUNTER_NAME; bufferSize *= 2) {\n                pcchNameBufferSize = new DWORDByReference(new DWORD(bufferSize));\n                mem = new Memory(bufferSize * CHAR_TO_BYTES);\n                result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, mem, pcchNameBufferSize);\n                if (result != PdhMsg.PDH_INVALID_ARGUMENT && result != PdhMsg.PDH_INSUFFICIENT_BUFFER) {\n                    break;\n                }\n            }","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/PdhUtil.java#L53-L89","documentation":"PdhUtil.PdhLookupPerfNameByIndex translates a performance-counter index to its localized name via the PDH API. It wraps native return codes into PdhException; at this point the first (size-query) call returned a code that is neither ERROR_SUCCESS, PDH_MORE_DATA, nor PDH_INVALID_ARGUMENT, so the failure is raised immediately with the raw code.","triggerScenarios":"Calling PdhLookupPerfNameByIndex with an index that has no mapping in the current locale's counter table, or on a system where the PDH/performance counter registry data is damaged, returning codes like PDH_INVALID_HANDLE or PDH_CSTATUS_NO_MACHINE.","commonSituations":"Localizing counters across Windows versions/languages where index tables differ; missing or corrupt Perfc009.dat/PerfString.ini counter data; querying a machine (szMachineName) that is unreachable or has remote registry disabled.","solutions":["Check the numeric code carried by PdhException against the PDH error constants to identify the exact failure","Verify the counter index is valid for the target locale (compare against the counter table for that language)","Repair performance-counter registry data (lodctr /r) if the table is corrupt","If a remote machine name is passed, confirm the machine is reachable and remote performance monitoring is enabled"],"exampleFix":"// before\nString name = PdhUtil.PdhLookupPerfNameByIndex(null, index);\n// after\ntry {\n    String name = PdhUtil.PdhLookupPerfNameByIndex(null, index);\n} catch (PdhException e) {\n    LOG.warn(\"No localized perf name for index \" + index + \": \" + e.getErrorCode());\n    name = String.valueOf(index); // fallback\n}","handlingStrategy":"try-catch","validationCode":"if (index <= 0) throw new IllegalArgumentException(\"performance counter index must be positive\");","typeGuard":null,"tryCatchPattern":"try { return PdhUtil.PdhLookupPerfNameByIndex(null, index); } catch (PdhException e) { LOG.warn(\"PDH lookup failed, code \" + e.getErrorCode()); return null; }","preventionTips":["Confirm the index exists in the target locale's counter table","Repair counter data (lodctr /r) on machines with corrupt performance counters","Cache localized names; avoid lookups against machines known to lack the counter"],"tags":["windows","pdh","performance-counters","jni"],"backgroundTag":"api-error-response","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"}