jinterop Win32_Process创建

时间:2013-02-20 23:17:00

标签: java wmi win32-process j-interop

我正在尝试使用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]

知道可能出现什么问题吗?提前谢谢!

1 个答案:

答案 0 :(得分:3)

这是解决方案......

您不应该使用InstanceOf来获取Win32_Process,因为您将获得当前正在运行的进程的列表。相反,您应该使用“获取”来获取默认的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);
}