2

我正在尝试使用 j-interop 实现以下 wmic 命令。

wmic /NODE:192.168.0.195 /USER:Test /PASSWORD:password123 process call create "calc.exe"

我的代码在我的方法中是这样写的。我还有另外两种方法可以创建会话并连接到 WMI 服务,以便处理该部分。

public void wmiExecute() throws JIException {

    // Obtain Win32_Process and narrow it as IJIDispatch
    Object[] params = new Object[] {
        new JIString("Win32_Process"),
        new Integer(0),
        JIVariant.OPTIONAL_PARAM()
    };
    JIVariant[] servicesSet = this._wbemServices.callMethodA("InstancesOf", params);
    IJIDispatch wbemObjectSet = (IJIDispatch) JIObjectFactory.narrowObject(servicesSet[0].getObjectAsComObject());

    params = new Object[] {
            "calc.exe",
             JIVariant.OPTIONAL_PARAM(),
             JIVariant.OPTIONAL_PARAM(),
             new Integer(0),
    };
    wbemObjectSet.callMethodA("Create", params);
}

我一直得到一个例外

Caught Throwable: org.jinterop.dcom.common.JIException: Unknown name. [0x80020006]
org.jinterop.dcom.common.JIException: Unknown name. [0x80020006]

知道有什么问题吗?提前致谢!

4

1 回答 1

3

这是解决方案...

您不应该使用 InstanceOf 来获取 Win32_Process ,因为您只会获得当前正在运行的进程的列表。相反,您应该使用“Get”来获取默认的 Win32_Process。

public void wmiExecute() throws JIException {

    // Obtain Win32_Process and narrow it as IJIDispatch
    Object[] params = new Object[] {
        new JIString("Win32_Process"),
        JIVariant.OPTIONAL_PARAM(),
        JIVariant.OPTIONAL_PARAM()
    };

    // Obtain the default Win32_Process
    JIVariant[] service = this._wbemServices.callMethodA("Get", params);

    // Convert it to a IJIDispatch object
    IJIDispatch wbemObject = (IJIDispatch) JIObjectFactory.narrowObject(service[0].getObjectAsComObject());

    // Create input params
    Object[] paramsCalc = new Object[] {
             new JIString("calc.exe"),
             JIVariant.OPTIONAL_PARAM(),
             JIVariant.OPTIONAL_PARAM()
    };

    // Create the calculator process
    JIVariant[] results = wbemObject.callMethodA("Create", paramsCalc);
}
于 2013-02-26T16:31:18.637 回答