{"record":{"id":"52c070ce5b10f84e","repo":"java-native-access/jna","slug":"failed-to-create-wbemlocator-object","errorCode":null,"errorMessage":"Failed to create WbemLocator object.","messagePattern":"Failed to create WbemLocator object\\.","errorType":"exception","errorClass":"com.sun.jna.platform.win32.COM.COMException","httpStatus":null,"severity":"critical","filePath":"contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java","lineNumber":525,"sourceCode":"     * and received.\n     */\n\n    /**\n     * Obtains a locator to the WMI server and connects to the specified\n     * namespace\n     *\n     * @param namespace\n     *            The namespace to connect to\n     * @return A service representing the connected namespace, which can be\n     *         queried. This service may be re-used for multiple queries and\n     *         should be released by the user\n     */\n    public static IWbemServices connectServer(String namespace) {\n        // Step 3: ---------------------------------------------------\n        // Obtain the initial locator to WMI -------------------------\n        IWbemLocator loc = IWbemLocator.create();\n        if (loc == null) {\n            throw new COMException(\"Failed to create WbemLocator object.\");\n        }\n\n        // Step 4: -----------------------------------------------------\n        // Connect to WMI through the IWbemLocator::ConnectServer method\n        // Connect to the namespace with the current user and obtain pointer\n        // pSvc to make IWbemServices calls.\n        IWbemServices services = loc.ConnectServer(namespace, null, null, null, 0, null, null);\n        // Release the locator. If successful, pSvc contains connection\n        // information\n        loc.Release();\n\n        // Step 5: --------------------------------------------------\n        // Set security levels on the proxy -------------------------\n        HRESULT hres = Ole32.INSTANCE.CoSetProxyBlanket(services, Ole32.RPC_C_AUTHN_WINNT, Ole32.RPC_C_AUTHZ_NONE, null,\n                Ole32.RPC_C_AUTHN_LEVEL_CALL, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32.EOAC_NONE);\n        if (COMUtils.FAILED(hres)) {\n            services.Release();\n            throw new COMException(\"Could not set proxy blanket.\", hres);","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java#L507-L543","documentation":"connectServer obtains an IWbemLocator via IWbemLocator.create() (CoCreateInstance under the hood). If COM instantiation fails and returns null, a COMException is thrown because WMI connections cannot proceed without a locator object.","triggerScenarios":"IWbemLocator.create() returns null because COM was not initialized on the thread (missing CoInitializeEx/CoInitializeSecurity), the COM subsystem failed, or the WbemLocator COM class is not registered.","commonSituations":"Calling WMI APIs on a thread where Ole32 initialization was skipped; running in an environment with broken COM registration; using the library after a COMException left COM in a bad state.","solutions":["Ensure Ole32.INSTANCE.CoInitializeEx(...) (and CoInitializeSecurity where required) was called on the current thread before connectServer.","Verify the WMI/COM installation on the machine; test with PowerShell Get-WmiObject.","Catch COMException and reinitialize COM on the calling thread if a previous failure may have left it uninitialized.","Run from a process with a proper COM apartment (STA/MTA as required by the library docs)."],"exampleFix":"// before\nIWbemServices svc = WbemcliUtil.connectServer(namespace); // thread never initialized COM\n// after\nHANDLE h = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);\nIWbemServices svc = WbemcliUtil.connectServer(namespace);","handlingStrategy":"validation","validationCode":"HANDLE hr = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);\n// proceed with WMI calls only after COM init succeeds on this thread","typeGuard":null,"tryCatchPattern":"try {\n    IWbemServices svc = WbemcliUtil.connectServer(ns);\n} catch (COMException e) {\n    if (e.getMessage().contains(\"WbemLocator\")) { /* initialize COM, then retry once */ }\n}","preventionTips":["Always CoInitializeEx on the thread before WMI calls","Avoid calling WMI from threads with unknown COM state","Verify COM/WMI installation in deployment checks"],"tags":["windows","wmi","com","jna","initialization"],"backgroundTag":"module-init-failed","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"}