使用JNA和Java调用iwbemservices.getObject()?
我希望将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是撰写该类的人,最初是在我自己的项目中,后来又为JNA做出了贡献,而我仅映射了执行WMI查询所需的功能。
查看
execquery()
的映射方式,并与新方法签名做类似的操作。您需要的重要信息是指向您要映射功能的指针的索引。您可以在注释中看到execquery()
是21个方法(索引20)。查找
wbemcli.h
的副本,并在其中找到iwbemservicesvtbl
。在这里,您可以看到
queryInterface
,addref
和版本
是索引0、1和2(并且可以在未知中使用
超类)。getObject
是索引6(列出的第七个功能)。因此,您可以使用自己的JNA的IWBEMServices类扩展JNA的IWBEMServices类,并使用JNA类添加新方法来复制签名。 Note
Windows中的Long
在映射时类似于
execquery
的映射时,您可能希望创建一个助手功能来包装实例化并释放> BSTR
,测试返回值,并在PointerByReference
中提取您收到的信息。JNA是一个用户维护的项目。为了帮助其他用户,请随时向JNA提交PR以贡献您的新映射,以便下一个人不需要完成所有这些工作(然后您可以在下一个版本中使用JNA使用它)!
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.
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 thatExecQuery()
is the 21st method (index 20).Look for a copy of
WbemCli.h
and find theIWbemServicesVtbl
in it. I found a copy here.Here you see
QueryInterface
,AddRef
, andRelease
are indices 0, 1, and 2 (and are available to you in theUnknown
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 itSimilar to the mapping of
ExecQuery
you may wish to create a helper function to wrap the instantiation and freeing of theBSTR
, test the return value, and extract the information you receive in thePointerByReference
.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)!