使用 Jacob 和 WMI 编写更好的代码

发布于 2024-08-12 19:03:45 字数 807 浏览 5 评论 0原文

我正在使用 JACOB 通过 WMI 访问系统信息。我在网上没有找到太多关于 WMI 和 Jacob 的文档,并且想知道是否可以获得一些帮助来使代码更高效。

这是代码:

ActiveXComponent mActiveXWMI = new ActiveXComponent("winmgmts:\\\\localhost\\root\\CIMV2");
String query = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'";
Variant vCollection = mActiveXWMI.invoke("ExecQuery", new Variant(query));

EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
    item = enumVariant.nextElement().toDispatch();
    cpuUsage = Double.parseDouble(Dispatch.call(item, "PercentProcessorTime").toString());
}

正如您所看到的,仅循环遍历集合中的一项似乎没有多大意义。我只想查询查询语句中的一列,并尽可能快速有效地获取结果,并尽可能减少开销。

是否有人对 JACOB 有丰富的经验并以尽可能最好的方式检索这些值?

谢谢,

史蒂夫

I am using JACOB to access system information through WMI. I have not found much documentation for WMI and Jacob on the web and was wondering if I could get some help in making the code a little more efficient.

Here is the code:

ActiveXComponent mActiveXWMI = new ActiveXComponent("winmgmts:\\\\localhost\\root\\CIMV2");
String query = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'";
Variant vCollection = mActiveXWMI.invoke("ExecQuery", new Variant(query));

EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
    item = enumVariant.nextElement().toDispatch();
    cpuUsage = Double.parseDouble(Dispatch.call(item, "PercentProcessorTime").toString());
}

As one can see, it doesn't seem to make much sense of looping through a collection for just one item. I would like to just query for one column in the query statement and get the result from that as quickly and efficiently as possible, with as little overhead as possible.

Does anyone have much experience with JACOB and retrieving these values in the best way possible?

Thanks,

Steve

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

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

发布评论

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

评论(1

动次打次papapa 2024-08-19 19:03:45

我的理解是,一般来说,WMI 始终会为任何 ExecQuery 返回零个或多个项目的集合。如果 JACOB 的 EnumVariant 类是从 WMI 接收信息的最佳方式(从我见过的示例中),那么您需要以一种或另一种方式枚举它。

(您也许可以将更多行压缩在一起,例如 EnumVariant enumVariant = new EnumVariant( mActiveXWMI.invoke("ExecQuery", new Variant(query)).toDispatch() ); - 但是这使得它更难阅读,并且不会帮助性能或任何东西。)

如果您确定查询将返回不超过一项(如您的示例中所示),您可以更改“while”到“if”语句(然后处理“else”子句中失败的情况)。

但除此之外……我认为它不会比你已经有的短很多。

My understanding is that in general, WMI will always return a collection of zero or more items for any ExecQuery. And if JACOB's EnumVariant class is the best way to receive the info from WMI (from the examples I've seen), then you need to enumerate through it in one way or another.

(You might be able to compress a few more lines together, like EnumVariant enumVariant = new EnumVariant( mActiveXWMI.invoke("ExecQuery", new Variant(query)).toDispatch() );--but that makes it even harder to read, and won't help performance or anything.)

If you're certain the query will return no more than one item--as in your example--you could change the "while" to an "if" statement (and then handle the case where it fails in your "else" clause).

But otherwise... I don't think it's going to get much shorter than what you have already.

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