java-native-access/jna · error · IllegalArgumentException

Invalid guid length:

Error message

Invalid guid length: 

What it means

GUID.fromString throws this IllegalArgumentException when the string representation of the GUID is longer than 38 characters (the maximum length of the '{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}' form). Longer inputs cannot be a valid GUID string.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Guid.java:261

        }

        /**
         * From string.
         *
         * @param guid
         *            the guid
         * @return the guid
         */
        public static GUID fromString(String guid) {
            int y = 0;
            char[] _cnewguid = new char[32];
            char[] _cguid = guid.toCharArray();
            byte[] bdata = new byte[16];
            GUID newGuid = new GUID();

            // we not accept a string longer than 38 chars
            if (guid.length() > 38) {
                throw new IllegalArgumentException("Invalid guid length: "
                        + guid.length());
            }

            // remove '{', '}' and '-' from guid string
            for (int i = 0; i < _cguid.length; i++) {
                if ((_cguid[i] != '{') && (_cguid[i] != '-')
                        && (_cguid[i] != '}'))
                    _cnewguid[y++] = _cguid[i];
            }

            // convert char to byte
            for (int i = 0; i < 32; i += 2) {
                bdata[i / 2] = (byte) ((Character.digit(_cnewguid[i], 16) << 4)
                        + Character.digit(_cnewguid[i + 1], 16) & 0xff);
            }

            if (bdata.length != 16) {
                throw new IllegalArgumentException("Invalid data length: "

View on GitHub (pinned to d036ad9781)

Solutions

  1. Trim and strip surrounding text so only the 38-char '{...-...}' form (or 32/36-char variant) remains
  2. Extract the GUID with a regex like \{?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}\}? before parsing
  3. If the input is a raw hex string without dashes/braces, ensure it is exactly 32 hex chars

Example fix

// before
GUID g = GUID.fromString(regValue); // "CLSID\{6B29...}-extra"
// after
java.util.regex.Matcher m = java.util.regex.Pattern
    .compile("\\{?[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\\}?")
    .matcher(regValue);
GUID g = m.find() ? GUID.fromString(m.group()) : null;
Defensive patterns

Strategy: validation

Validate before calling

String s = raw.trim();
if (s.length() > 38)
    throw new IllegalArgumentException("Not a GUID string (len " + s.length() + "): " + raw);
GUID g = GUID.fromString(s);

Type guard

boolean isGuidShape(String s) {
    return s != null && s.length() <= 38 && s.matches("\\{?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}\\}?");
}

Try / catch

try {
    GUID g = GUID.fromString(input);
} catch (IllegalArgumentException e) {
    // strip surrounding text or extract via regex, then retry
    java.util.regex.Matcher m = GUID_RX.matcher(input);
    GUID g = m.find() ? GUID.fromString(m.group()) : null;
}

Prevention

When it happens

Trigger: Passing a full IID string with extra whitespace/prefix, a base64 or hex-dumped GUID (e.g. 32-byte string), or concatenating GUIDs/typo (duplicate paste) into fromString.

Common situations: Reading identifiers from registry/config that include a prefix like 'CLSID\{...}' or trailing data; accidentally passing a byte-hex dump of two GUIDs; UI copy including surrounding text.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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