{"record":{"id":"9e2af8d3079380c9","repo":"java-native-access/jna","slug":"unexpected-registry-type-expected-reg-qword","errorCode":null,"errorMessage":"Unexpected registry type {}, expected REG_QWORD","messagePattern":"Unexpected registry type (.+?), expected REG_QWORD","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java","lineNumber":1227,"sourceCode":"     * Get a registry QWORD 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 long registryGetLongValue(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_QWORD) {\n            throw new RuntimeException(\"Unexpected registry type \"\n                    + lpType.getValue() + \", expected REG_QWORD\");\n        }\n        LongByReference data = new LongByReference();\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 value and returns a java object depending on the value\n     * type.\n     *\n     * @param hkKey\n     *            Root key.","sourceCodeStart":1209,"sourceCodeEnd":1245,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java#L1209-L1245","documentation":"registryGetLongValue queries the value's type via RegQueryValueEx and throws this RuntimeException if it is not WinNT.REG_QWORD. The guard ensures the returned 64-bit long is only produced from genuine QWORD data. Note the underlying Windows API returns REG_QWORD data as an 8-byte value that the library reads into a LongByReference.","triggerScenarios":"Calling Advapi32Util.registryGetLongValue(key, valueName) (or root/key/value overload) on a value stored as REG_DWORD, REG_SZ, REG_BINARY or any type other than REG_QWORD.","commonSituations":"A timestamp or large counter was written by a legacy tool as two REG_DWORDs or as a string; reading a REG_DWORD value with the 64-bit getter; documentation drift where the stored type changed between product versions.","solutions":["Use registryGetIntValue for REG_DWORD values, or registryGetStringValue + Long.parseLong for string-stored numbers.","Read via registryGetValue and convert according to the runtime type (Integer vs Long vs String).","Rewrite the value as REG_QWORD if you control the writer (PowerShell -PropertyType QWord).","Catch RuntimeException and fall back to a type-dispatched read."],"exampleFix":"// before\nlong ts = Advapi32Util.registryGetLongValue(root, key, \"LastRun\");\n\n// after\nObject v = Advapi32Util.registryGetValue(root, key, \"LastRun\");\nlong ts = (v instanceof Long) ? (Long) v\n    : (v instanceof Integer) ? ((Integer) v).longValue()\n    : Long.parseLong(String.valueOf(v));","handlingStrategy":"validation","validationCode":"Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);\nif (!(v instanceof Long)) throw new IllegalStateException(\"Expected REG_QWORD for \" + valueName);","typeGuard":"boolean isQword(Object v) { return v instanceof Long; }","tryCatchPattern":"try { return Advapi32Util.registryGetLongValue(root, key, value); }\ncatch (RuntimeException e) {\n  Object v = Advapi32Util.registryGetValue(root, key, value);\n  return (v instanceof Number) ? ((Number) v).longValue() : Long.parseLong(String.valueOf(v));\n}","preventionTips":["Match the getter width to the stored type (int getter for DWORD, long getter for QWORD)","Write QWORDs with -PropertyType QWord","Coerce strings with Long.parseLong explicitly","Document the expected registry schema per value name"],"tags":["windows-registry","type-mismatch","reg-qword","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"}