java-native-access/jna · critical · com.sun.jna.platform.win32.COM.COMException

Failed to create WbemLocator object.

Error message

Failed to create WbemLocator object.

What it means

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.

Source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java:525

     * and received.
     */

    /**
     * Obtains a locator to the WMI server and connects to the specified
     * namespace
     *
     * @param namespace
     *            The namespace to connect to
     * @return A service representing the connected namespace, which can be
     *         queried. This service may be re-used for multiple queries and
     *         should be released by the user
     */
    public static IWbemServices connectServer(String namespace) {
        // Step 3: ---------------------------------------------------
        // Obtain the initial locator to WMI -------------------------
        IWbemLocator loc = IWbemLocator.create();
        if (loc == null) {
            throw new COMException("Failed to create WbemLocator object.");
        }

        // Step 4: -----------------------------------------------------
        // Connect to WMI through the IWbemLocator::ConnectServer method
        // Connect to the namespace with the current user and obtain pointer
        // pSvc to make IWbemServices calls.
        IWbemServices services = loc.ConnectServer(namespace, null, null, null, 0, null, null);
        // Release the locator. If successful, pSvc contains connection
        // information
        loc.Release();

        // Step 5: --------------------------------------------------
        // Set security levels on the proxy -------------------------
        HRESULT hres = Ole32.INSTANCE.CoSetProxyBlanket(services, Ole32.RPC_C_AUTHN_WINNT, Ole32.RPC_C_AUTHZ_NONE, null,
                Ole32.RPC_C_AUTHN_LEVEL_CALL, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32.EOAC_NONE);
        if (COMUtils.FAILED(hres)) {
            services.Release();
            throw new COMException("Could not set proxy blanket.", hres);

View on GitHub (pinned to d036ad9781)

Solutions

  1. Ensure Ole32.INSTANCE.CoInitializeEx(...) (and CoInitializeSecurity where required) was called on the current thread before connectServer.
  2. Verify the WMI/COM installation on the machine; test with PowerShell Get-WmiObject.
  3. Catch COMException and reinitialize COM on the calling thread if a previous failure may have left it uninitialized.
  4. Run from a process with a proper COM apartment (STA/MTA as required by the library docs).

Example fix

// before
IWbemServices svc = WbemcliUtil.connectServer(namespace); // thread never initialized COM
// after
HANDLE h = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
IWbemServices svc = WbemcliUtil.connectServer(namespace);
Defensive patterns

Strategy: validation

Validate before calling

HANDLE hr = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
// proceed with WMI calls only after COM init succeeds on this thread

Try / catch

try {
    IWbemServices svc = WbemcliUtil.connectServer(ns);
} catch (COMException e) {
    if (e.getMessage().contains("WbemLocator")) { /* initialize COM, then retry once */ }
}

Prevention

When it happens

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

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

Related errors


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