{"record":{"id":"4eec1ac608b02829","repo":"java-native-access/jna","slug":"stringfromguid2","errorCode":null,"errorMessage":"StringFromGUID2","messagePattern":"StringFromGUID2","errorType":"error_code","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Ole32Util.java","lineNumber":65,"sourceCode":"            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\");\n        }\n        lpsz[len - 1] = 0;\n        return Native.toString(lpsz);\n    }\n\n    /**\n     * Generate a new GUID.\n     *\n     * @return New GUID.\n     */\n    public static GUID generateGUID() {\n        GUID pguid = new GUID();\n        HRESULT hr = Ole32.INSTANCE.CoCreateGuid(pguid);\n        if (!hr.equals(W32Errors.S_OK)) {\n            throw new RuntimeException(hr.toString());\n        }\n        return pguid;\n    }","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Ole32Util.java#L47-L83","documentation":"Ole32Util.getStringFromGUID converts a native GUID structure to its string form via Ole32 StringFromGUID2, which returns 0 when the supplied buffer is too small. The library throws a bare RuntimeException with the literal message 'StringFromGUID2' when the returned length is 0.","triggerScenarios":"StringFromGUID2 returning 0, which in practice happens only if the char buffer is smaller than 39 characters; the library hardcodes max=39 so this is nearly unreachable from normal use — it signals a native/environmental failure of the COM conversion call.","commonSituations":"Rare: corrupted COM runtime state, exotic JVM/native setups where the fixed 39-char buffer assumption fails, or a bug in a forked/modified version of the helper that reduced the buffer size.","solutions":["Verify no modified/forked Ole32Util is in use where the buffer size was changed below 39","Retry with a fresh GUID object; if persistent, check that the COM runtime (ole32.dll) is healthy on the host","Catch the RuntimeException and treat it as an unexpected native failure rather than an input problem"],"exampleFix":"// before\nString s = Ole32Util.getStringFromGUID(guid);\n// after\nString s;\ntry {\n    s = Ole32Util.getStringFromGUID(guid);\n} catch (RuntimeException e) {\n    throw new IllegalStateException(\"StringFromGUID2 conversion failed\", e);\n}","handlingStrategy":"try-catch","validationCode":"if (guid == null || guid.getPointer() == null) throw new IllegalArgumentException(\"GUID must be initialized\");","typeGuard":"boolean isValidGuid(GUID g) { return g != null && g.getPointer() != null; }","tryCatchPattern":"try { return Ole32Util.getStringFromGUID(guid); } catch (RuntimeException e) { throw new IllegalStateException(\"StringFromGUID2 native conversion failed\", e); }","preventionTips":["Treat this as an environment-level failure, not an input problem","Use stock JNA platform code — do not reduce the 39-char buffer in forks","Log the GUID object state when it occurs to aid native debugging"],"tags":["windows","com","guid","jni"],"backgroundTag":"internal-invariant-violation","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"}