我的应用程序需要使用java列出计算机上所有可用的恢复点(链接)。SystemRestore类位于默认命名空间中,而不在CIMV2中。当我尝试以下代码时:
public class TestWMI {
public static void main(String args[]){
String host = "localhost";
String connectStr = String.format("winmgmts:\\%s\root\default", host);
String query = "SELECT * FROM SystemRestore";
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
item = enumVariant.nextElement().toDispatch();
String serviceName = Dispatch.call(item,"Description").toString();
System.out.println();
}
}
}
但它最终会出现以下错误:
Exception in thread "main" com.jacob.com.ComFailException: IEnumVARIANT::Next
at com.jacob.com.EnumVariant.Next(Native Method)
at com.jacob.com.EnumVariant.hasMoreElements(EnumVariant.java:68)
at TestWMI.main(TestWMI.java:28)
Java Result: 1
请帮忙。
这里我们有一篇文章说,这个错误可能是由没有以管理员身份运行引起的。
这里有一个例子,您通过查询外部数据WMI别名得到了相同的错误,但在使用select
时应该查询全名。
SELECT Index,InterfaceIndex,SettingID,IpAddress,ServiceName,Description
FROM NICCONFIG
WHERE IPEnabled=true
应该是:
SELECT Index,InterfaceIndex,SettingID,IpAddress,ServiceName,Description
FROM Win32_NetworkAdapterConfiguration
WHERE IPEnabled=true
以下是使用外部别名(来自命令行)或全名(来自WMI API调用)的指南。