{"record":{"id":"ce6a3efbc973b3a6","repo":"java-native-access/jna","slug":"unexpected-registry-type-expected-reg-binary","errorCode":null,"errorMessage":"Unexpected registry type {}, expected REG_BINARY","messagePattern":"Unexpected registry type (.+?), expected REG_BINARY","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java","lineNumber":1071,"sourceCode":"     * Get a registry REG_BINARY value.\n     *\n     * @param hKey\n     *            Parent Key.\n     * @param value\n     *            Name of the value to retrieve.\n     * @return String value.\n     */\n    public static byte[] registryGetBinaryValue(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, (Pointer) 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_BINARY) {\n            throw new RuntimeException(\"Unexpected registry type \"\n                    + lpType.getValue() + \", expected REG_BINARY\");\n        }\n        byte[] data = new byte[lpcbData.getValue()];\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;\n    }\n\n    /**\n     * Get a registry DWORD value.\n     *\n     * @param root\n     *            Root key.\n     * @param key","sourceCodeStart":1053,"sourceCodeEnd":1089,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java#L1053-L1089","documentation":"registryGetBinaryValue first calls RegQueryValueEx with a NULL buffer to learn the value size and type, then throws this RuntimeException if the reported type is not WinNT.REG_BINARY. The library refuses to return arbitrary registry data as a byte[] when the OS says the value is of another type, so callers do not silently misinterpret DWORDs or strings as raw bytes.","triggerScenarios":"Calling Advapi32Util.registryGetBinaryValue(key, valueName) (or root/key/value overload) on a value stored as REG_SZ, REG_DWORD, REG_EXPAND_SZ or any type other than REG_BINARY.","commonSituations":"A binary blob was later managed by another tool that rewrote it as a string; assuming a GUID or packed struct value is REG_BINARY when the writer stored it as REG_SZ; schema drift between application versions.","solutions":["Read the value with registryGetValue (type-dispatching) and convert to byte[] only if it is byte[].","Use the typed getter matching the actual type (registryGetStringValue, registryGetIntValue, ...).","Recreate the value with type REG_BINARY (PowerShell New-ItemProperty -PropertyType Binary) if you control the writer.","Catch RuntimeException and log the actual type value from the message before choosing a fallback."],"exampleFix":"// before\nbyte[] blob = Advapi32Util.registryGetBinaryValue(root, key, \"Config\");\n\n// after\nObject v = Advapi32Util.registryGetValue(root, key, \"Config\");\nbyte[] blob = (v instanceof byte[]) ? (byte[]) v\n    : (v instanceof String) ? ((String) v).getBytes(StandardCharsets.UTF_8)\n    : null;","handlingStrategy":"validation","validationCode":"Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);\nif (!(v instanceof byte[])) throw new IllegalStateException(\"Expected REG_BINARY for \" + valueName);","typeGuard":"boolean isBinary(Object v) { return v instanceof byte[]; }","tryCatchPattern":"try { return Advapi32Util.registryGetBinaryValue(root, key, value); }\ncatch (RuntimeException e) { log.warn(\"Value {} is not REG_BINARY\", value); return new byte[0]; }","preventionTips":["Persist blobs with -PropertyType Binary / REG_BINARY","Convert with an explicit encoding when the writer used REG_SZ","Validate stored type once at startup, not per read","Prefer registryGetValue for values of uncertain type"],"tags":["windows-registry","type-mismatch","reg-binary","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"}