{"record":{"id":"b17e588888271b6f","repo":"java-native-access/jna","slug":"runtimeexception-hr-tostring","errorCode":null,"errorMessage":"RuntimeException(hr.toString())","messagePattern":"RuntimeException\\(hr\\.toString\\(\\)\\)","errorType":"error_code","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Ole32Util.java","lineNumber":47,"sourceCode":"\n/**\n * Ole32 Utility API.\n * @author dblock[at]dblock.org\n */\npublic abstract class Ole32Util {\n\n    /**\n     * Convert a string to a GUID.\n     *\n     * @param guidString String representation of a GUID, including { }.\n     *\n     * @return A GUID.\n     */\n    public static GUID getGUIDFromString(String guidString) {\n        GUID lpiid = new GUID();\n        HRESULT hr = Ole32.INSTANCE.IIDFromString(guidString, lpiid);\n        if (!hr.equals(W32Errors.S_OK)) {\n            throw new RuntimeException(hr.toString());\n        }\n        return lpiid;\n    }\n\n    /**\n     * Convert a GUID into a string.\n     *\n     * @param guid GUID.\n     *\n     * @return String representation of a GUID.\n     */\n    public static String getStringFromGUID(GUID guid) {\n        GUID pguid = new GUID(guid.getPointer());\n        int max = 39;\n        char[] lpsz = new char[max];\n        int len = Ole32.INSTANCE.StringFromGUID2(pguid, lpsz, max);\n        if (len == 0) {\n            throw new RuntimeException(\"StringFromGUID2\");","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Ole32Util.java#L29-L65","documentation":"Ole32Util.getGUIDFromString parses a GUID/CLSID string (e.g. '{XXXXXXXX-XXXX-...}') into a native GUID structure via Ole32 IIDFromString. If the native call returns any HRESULT other than S_OK, the library throws a bare RuntimeException whose message is just the HRESULT toString (e.g. '0x80004005'), losing the context that GUID parsing failed.","triggerScenarios":"Calling getGUIDFromString with a string that is not a well-formed GUID: missing braces, wrong digit/hex characters, wrong dash grouping, extra whitespace, or an empty string — any input IIDFromString cannot parse.","commonSituations":"Hardcoded CLSIDs copied from documentation or IDL files with typos; GUID strings read from config/registry that were stored in a different format (no braces, lowercase '0x' prefixes); user-supplied class IDs passed through from application config.","solutions":["Fix the input string to be a valid GUID in the form {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} (braces optional but grouping and hex digits must be correct)","Validate the string against a GUID regex before calling getGUIDFromString","Catch the RuntimeException and inspect the message HRESULT to confirm ERROR_INVALID_PARAMETER-style failure meaning malformed input"],"exampleFix":"// before\nGUID guid = Ole32Util.getGUIDFromString(guidString);\n// after\nif (!guidString.matches(\"^\\\\{?[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}\\\\}?$\")) {\n    throw new IllegalArgumentException(\"Not a valid GUID string: \" + guidString);\n}\nGUID guid = Ole32Util.getGUIDFromString(guidString);","handlingStrategy":"validation","validationCode":"private static final java.util.regex.Pattern GUID = 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}\\\\}?$\");\nif (guidString == null || !GUID.matcher(guidString.trim()).matches()) throw new IllegalArgumentException(\"invalid GUID: \" + guidString);","typeGuard":"boolean isGuidString(String s) { return s != null && GUID.matcher(s.trim()).matches(); }","tryCatchPattern":"try { GUID g = Ole32Util.getGUIDFromString(s); } catch (RuntimeException e) { throw new IllegalArgumentException(\"malformed GUID string: \" + s, e); }","preventionTips":["Validate GUID format with a regex before native parsing","Trim whitespace and normalize case/braces of GUID strings from config","Never pass user input directly to getGUIDFromString without validation"],"tags":["windows","com","guid","jni"],"backgroundTag":"invalid-argument-format","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"}