java-native-access/jna · error · RuntimeException
Unexpected registry type {}, expected REG_BINARY
Error message
Unexpected registry type {}, expected REG_BINARY What it means
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.
Source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java:1071
* Get a registry REG_BINARY value.
*
* @param hKey
* Parent Key.
* @param value
* Name of the value to retrieve.
* @return String value.
*/
public static byte[] registryGetBinaryValue(HKEY hKey, String value) {
IntByReference lpcbData = new IntByReference();
IntByReference lpType = new IntByReference();
int rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
lpType, (Pointer) null, lpcbData);
if (rc != W32Errors.ERROR_SUCCESS
&& rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(rc);
}
if (lpType.getValue() != WinNT.REG_BINARY) {
throw new RuntimeException("Unexpected registry type "
+ lpType.getValue() + ", expected REG_BINARY");
}
byte[] data = new byte[lpcbData.getValue()];
rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
lpType, data, lpcbData);
if (rc != W32Errors.ERROR_SUCCESS
&& rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(rc);
}
return data;
}
/**
* Get a registry DWORD value.
*
* @param root
* Root key.
* @param keyView on GitHub (pinned to d036ad9781)
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.
Example fix
// before
byte[] blob = Advapi32Util.registryGetBinaryValue(root, key, "Config");
// after
Object v = Advapi32Util.registryGetValue(root, key, "Config");
byte[] blob = (v instanceof byte[]) ? (byte[]) v
: (v instanceof String) ? ((String) v).getBytes(StandardCharsets.UTF_8)
: null; Defensive patterns
Strategy: validation
Validate before calling
Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);
if (!(v instanceof byte[])) throw new IllegalStateException("Expected REG_BINARY for " + valueName); Type guard
boolean isBinary(Object v) { return v instanceof byte[]; } Try / catch
try { return Advapi32Util.registryGetBinaryValue(root, key, value); }
catch (RuntimeException e) { log.warn("Value {} is not REG_BINARY", value); return new byte[0]; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Unexpected registry type + lpType.getValue() + , expected RE
- Unexpected registry type {}, expected REG_SZ
- Unexpected registry type {}, expected REG_DWORD
- Unexpected registry type {}, expected REG_QWORD
- Unexpected registry type + lpType.getValue() + , expected RE
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/ce6a3efbc973b3a6.
Report an issue: GitHub.