{"record":{"id":"cf1f0a38a4a3ae32","repo":"java-native-access/jna","slug":"unexpected-registry-type-expected-reg-dword","errorCode":null,"errorMessage":"Unexpected registry type {}, expected REG_DWORD","messagePattern":"Unexpected registry type (.+?), expected REG_DWORD","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java","lineNumber":1149,"sourceCode":"     * Get a registry DWORD value.\n     *\n     * @param hKey\n     *            Parent key.\n     * @param value\n     *            Name of the value to retrieve.\n     * @return Integer value.\n     */\n    public static int registryGetIntValue(HKEY hKey, String value) {\n        IntByReference lpcbData = new IntByReference();\n        IntByReference lpType = new IntByReference();\n        int rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,\n                lpType, (char[]) null, lpcbData);\n        if (rc != W32Errors.ERROR_SUCCESS\n                && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {\n            throw new Win32Exception(rc);\n        }\n        if (lpType.getValue() != WinNT.REG_DWORD) {\n            throw new RuntimeException(\"Unexpected registry type \"\n                    + lpType.getValue() + \", expected REG_DWORD\");\n        }\n        IntByReference data = new IntByReference();\n        rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,\n                lpType, data, lpcbData);\n        if (rc != W32Errors.ERROR_SUCCESS\n                && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {\n            throw new Win32Exception(rc);\n        }\n        return data.getValue();\n    }\n\n    /**\n     * Get a registry QWORD value.\n     *\n     * @param root\n     *            Root key.\n     * @param key","sourceCodeStart":1131,"sourceCodeEnd":1167,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java#L1131-L1167","documentation":"registryGetIntValue sizes the value via RegQueryValueEx and throws this RuntimeException when the reported type is not WinNT.REG_DWORD. The library only decodes 32-bit integer data from values the OS reports as REG_DWORD, so callers never get garbage integers from strings or binary blobs.","triggerScenarios":"Calling Advapi32Util.registryGetIntValue(key, valueName) (or root/key/value overload) on a value whose type is REG_SZ, REG_BINARY or otherwise not REG_DWORD. Also commonly hit when the value was written as REG_QWORD but read with the 32-bit getter.","commonSituations":"A setting stored by a 64-bit tool as REG_QWORD read back with registryGetIntValue; a numeric-looking REG_SZ ('42') that was never actually a DWORD; group policy rewriting a value's type.","solutions":["Use registryGetLongValue if the value is REG_QWORD, or registryGetStringValue plus Integer.parseInt if it is REG_SZ.","Read the value with registryGetValue and convert based on the runtime type.","Rewrite the value as REG_DWORD (reg add /t REG_DWORD or PowerShell Set-ItemProperty with an int).","Catch RuntimeException, parse the actual type from the message or re-query the type, and dispatch."],"exampleFix":"// before\nint limit = Advapi32Util.registryGetIntValue(root, key, \"Limit\");\n\n// after\nObject v = Advapi32Util.registryGetValue(root, key, \"Limit\");\nint limit = (v instanceof Integer) ? (Integer) v\n    : (v instanceof Long) ? ((Long) v).intValue()\n    : Integer.parseInt(String.valueOf(v));","handlingStrategy":"validation","validationCode":"Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);\nif (!(v instanceof Integer)) throw new IllegalStateException(\"Expected REG_DWORD for \" + valueName + \", got \" + (v == null ? \"null\" : v.getClass().getSimpleName()));","typeGuard":"boolean isDword(Object v) { return v instanceof Integer; }","tryCatchPattern":"try { return Advapi32Util.registryGetIntValue(root, key, value); }\ncatch (RuntimeException e) {\n  Object v = Advapi32Util.registryGetValue(root, key, value);\n  return (v instanceof Number) ? ((Number) v).intValue() : Integer.parseInt(String.valueOf(v));\n}","preventionTips":["Write ints with Set-ItemProperty so the type is REG_DWORD","Use registryGetLongValue for REG_QWORD values","Parse string-stored numbers explicitly instead of expecting the getter to coerce","Check the type once via registryQueryInfoKey when the schema is fixed"],"tags":["windows-registry","type-mismatch","reg-dword","jna"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}