java-native-access/jna · error · RuntimeException

Unexpected registry type {}, expected REG_DWORD

Error message

Unexpected registry type {}, expected REG_DWORD

What it means

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.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java:1149

     * Get a registry DWORD value.
     *
     * @param hKey
     *            Parent key.
     * @param value
     *            Name of the value to retrieve.
     * @return Integer value.
     */
    public static int registryGetIntValue(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_DWORD) {
            throw new RuntimeException("Unexpected registry type "
                    + lpType.getValue() + ", expected REG_DWORD");
        }
        IntByReference data = new IntByReference();
        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 QWORD value.
     *
     * @param root
     *            Root key.
     * @param key

View on GitHub (pinned to d036ad9781)

Solutions

  1. Use registryGetLongValue if the value is REG_QWORD, or registryGetStringValue plus Integer.parseInt if it is REG_SZ.
  2. Read the value with registryGetValue and convert based on the runtime type.
  3. Rewrite the value as REG_DWORD (reg add /t REG_DWORD or PowerShell Set-ItemProperty with an int).
  4. Catch RuntimeException, parse the actual type from the message or re-query the type, and dispatch.

Example fix

// before
int limit = Advapi32Util.registryGetIntValue(root, key, "Limit");

// after
Object v = Advapi32Util.registryGetValue(root, key, "Limit");
int limit = (v instanceof Integer) ? (Integer) v
    : (v instanceof Long) ? ((Long) v).intValue()
    : Integer.parseInt(String.valueOf(v));
Defensive patterns

Strategy: validation

Validate before calling

Object v = Advapi32Util.registryGetValue(root, keyPath, valueName);
if (!(v instanceof Integer)) throw new IllegalStateException("Expected REG_DWORD for " + valueName + ", got " + (v == null ? "null" : v.getClass().getSimpleName()));

Type guard

boolean isDword(Object v) { return v instanceof Integer; }

Try / catch

try { return Advapi32Util.registryGetIntValue(root, key, value); }
catch (RuntimeException e) {
  Object v = Advapi32Util.registryGetValue(root, key, value);
  return (v instanceof Number) ? ((Number) v).intValue() : Integer.parseInt(String.valueOf(v));
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/cf1f0a38a4a3ae32. Report an issue: GitHub.