1

我的应用程序需要使用 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

请帮忙。

4

1 回答 1

0

在这里,我们有一个帖子说这个错误可能是由于没有以管理员身份运行引起的。

这是一个示例,您通过查询外部数据 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 调用)的指南。

于 2013-04-30T23:24:24.993 回答