使用JNA和Java调用iwbemservices.getObject()?

发布于 2025-01-30 02:30:30 字数 606 浏览 4 评论 0原文

我希望将JAVA语言中的WMI与JNA软件包一起使用。

我能够成功运行WQL查询:

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);

现在,我想使用其路径获得一个对象,其中类似:

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

不幸的是,方法getObject()未在JNA IWBEMSERVICES接口中定义。

为什么缺少此方法?如何称呼此方法?可能通过扩展IWBEMServices的界面,但是如何?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2025-02-06 02:30:30

为什么这种方法缺少

我是撰写该类的人,最初是在我自己的项目中,后来又为JNA做出了贡献,而我仅映射了执行WMI查询所需的功能。

我该如何称呼此方法?可能通过扩展iWbemServices的界面,但是如何?

查看execquery()的映射方式,并与新方法签名做类似的操作。您需要的重要信息是指向您要映射功能的指针的索引。您可以在注释中看到execquery()是21个方法(索引20)。

查找wbemcli.h的副本,并在其中找到iwbemservicesvtbl

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;

在这里,您可以看到queryInterfaceaddref版本是索引0、1和2(并且可以在未知中使用超类)。 getObject是索引6(列出的第七个功能)。

因此,您可以使用自己的JNA的IWBEMServices类扩展JNA的IWBEMServices类,并使用JNA类添加新方法来复制签名。 Note Windows中的Long在映射时

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);
}

类似于execquery的映射时,您可能希望创建一个助手功能来包装实例化并释放> BSTR,测试返回值,并在PointerByReference中提取您收到的信息。

JNA是一个用户维护的项目。为了帮助其他用户,请随时向JNA提交PR以贡献您的新映射,以便下一个人不需要完成所有这些工作(然后您可以在下一个版本中使用JNA使用它)!

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)!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文