java-native-access/jna · error · RuntimeException
Unexpected registry type {}, expected REG_QWORD
Error message
Unexpected registry type {}, expected REG_QWORD What it means
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.
Source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java:1227
* Get a registry QWORD value.
*
* @param hKey
* Parent key.
* @param value
* Name of the value to retrieve.
* @return Integer value.
*/
public static long registryGetLongValue(HKEY hKey, String value) {
IntByReference lpcbData = new IntByReference();
IntByReference lpType = new IntByReference();
int rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
lpType, (char[]) null, lpcbData);
if (rc != W32Errors.ERROR_SUCCESS
&& rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(rc);
}
if (lpType.getValue() != WinNT.REG_QWORD) {
throw new RuntimeException("Unexpected registry type "
+ lpType.getValue() + ", expected REG_QWORD");
}
LongByReference data = new LongByReference();
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.getValue();
}
/**
* Get a registry value and returns a java object depending on the value
* type.
*
* @param hkKey
* Root key.View on GitHub (pinned to d036ad9781)
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.
Example fix
// before
long ts = Advapi32Util.registryGetLongValue(root, key, "LastRun");
// after
Object v = Advapi32Util.registryGetValue(root, key, "LastRun");
long ts = (v instanceof Long) ? (Long) v
: (v instanceof Integer) ? ((Integer) v).longValue()
: Long.parseLong(String.valueOf(v)); Defensive patterns
Strategy: validation
Validate before calling
Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);
if (!(v instanceof Long)) throw new IllegalStateException("Expected REG_QWORD for " + valueName); Type guard
boolean isQword(Object v) { return v instanceof Long; } Try / catch
try { return Advapi32Util.registryGetLongValue(root, key, value); }
catch (RuntimeException e) {
Object v = Advapi32Util.registryGetValue(root, key, value);
return (v instanceof Number) ? ((Number) v).longValue() : Long.parseLong(String.valueOf(v));
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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_BINARY
- Unexpected registry type {}, expected REG_DWORD
- Unexpected registry type + lpType.getValue() + , expected RE
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/9e2af8d3079380c9.
Report an issue: GitHub.