1

I wish to use WMI from Java language with JNA package.

I am able to successfully run a WQL query:

import com.sun.jna.platform.win32.COM.Wbemcli;
Wbemcli.IWbemServices svc = WbemcliUtil.connectServer("ROOT\\CIMV2");
Wbemcli.IEnumWbemClassObject enumerator = svc.ExecQuery("WQL", "select Handle from Win32_Process", Wbemcli.WBEM_FLAG_FORWARD_ONLY, null);

Now, I would like to get an object using its path, with something like:

String object_path = "\\\\LAPTOP-R89KG6V1\\root\\cimv2:Win32_Process.Handle="2588";
svc.GetObject(object_path):

Unfortunately, the method GetObject() is not defined in JNA IWbemServices interface.

Why is it this method missing, and how can I call this method? Possibly by extending the interface of IWbemServices, but how?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
remi.chateauneu
  • 109
  • 2
  • 9

1 Answers1

1

Why is it this method missing

I'm the one who authored the class, originally in my own project and later contributed to JNA, and I only mapped the functions I needed to execute WMI Queries.

and how can I call this method? Possibly by extending the interface of IWbemServices, but how?

Look at how ExecQuery() is mapped and do something similar with the new method signature. The important piece of information you'll need is the index of the pointer to the function you're trying to map. You can see in the comments that ExecQuery() is the 21st method (index 20).

Look for a copy of WbemCli.h and find the IWbemServicesVtbl in it. I found a copy here.

typedef struct IWbemServicesVtbl
{
    BEGIN_INTERFACE
    HRESULT(STDMETHODCALLTYPE * QueryInterface) (IWbemServices * This, REFIID riid, void **ppvObject);
    ULONG(STDMETHODCALLTYPE * AddRef) (IWbemServices * This);
    ULONG(STDMETHODCALLTYPE * Release) (IWbemServices * This);
    HRESULT(STDMETHODCALLTYPE * OpenNamespace) (IWbemServices * This, const BSTR strNamespace, long lFlags, IWbemContext * pCtx, IWbemServices ** ppWorkingNamespace, IWbemCallResult ** ppResult);
    HRESULT(STDMETHODCALLTYPE * CancelAsyncCall) (IWbemServices * This, IWbemObjectSink * pSink);
    HRESULT(STDMETHODCALLTYPE * QueryObjectSink) (IWbemServices * This, long lFlags, IWbemObjectSink ** ppResponseHandler);
    HRESULT(STDMETHODCALLTYPE * GetObject) (IWbemServices * This, const BSTR strObjectPath, long lFlags, IWbemContext * pCtx, IWbemClassObject ** ppObject, IWbemCallResult ** ppCallResult);

    // other functions

    HRESULT(STDMETHODCALLTYPE * ExecMethod) (IWbemServices * This, const BSTR strObjectPath, const BSTR strMethodName, long lFlags, IWbemContext * pCtx, IWbemClassObject * pInParams, IWbemClassObject ** ppOutParams, IWbemCallResult ** ppCallResult);
    HRESULT(STDMETHODCALLTYPE * ExecMethodAsync) (IWbemServices * This, const BSTR strObjectPath, const BSTR strMethodName, long lFlags, IWbemContext * pCtx, IWbemClassObject * pInParams, IWbemObjectSink * pResponseHandler);
    END_INTERFACE
}  IWbemServicesVtbl;

Here you see QueryInterface, AddRef, and Release are indices 0, 1, and 2 (and are available to you in the Unknown superclass). GetObject is index 6 (the 7th function listed).

So you can extend JNA's IWbemServices class with your own and add the new method using JNA classes to replicate the signature. Note long in Windows is 32-bit when mapping it

public HRESULT GetObject(BSTR strObjectPath, int lFlags, IWbemContext pCtx, 
        PointerByReference ppObject, PointerByReference ppCallResult) {
    // GetObject is the 7th method of IWbemServicesVtbl in WbemCli.h
    return (HRESULT) _invokeNativeObject(6,
        new Object[] { getPointer(), strObjectPath, lFlags, pCtx,
        ppObject, ppCallResult}, HRESULT.class);
}

Similar to the mapping of ExecQuery you may wish to create a helper function to wrap the instantiation and freeing of the BSTR, test the return value, and extract the information you receive in the PointerByReference.

JNA is a user-maintained project. To help other users, please feel free to submit a PR to JNA to contribute your new mapping so the next person doesn't need to do all this work (and you can then use it from JNA in the next release)!

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63