jinterop Win32_Process Create

2

I am trying to achieve the following wmic command using j-interop.

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

I have my code written like this in my method. I have two other methods that create a session and connect to the WMI service so that part is taken care of.

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);
}

I kept getting an Exception of

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

Any idea what could be wrong? Thanks in advance!

java
wmi
win32-process
j-interop
asked on Stack Overflow Feb 20, 2013 by beyonddc

1 Answer

3

Here's the solution...

You shouldn't use InstanceOf to obtain Win32_Process because you will be just getting a list of currently running processes. Instead you should use "Get" to obtain the default 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);
}
answered on Stack Overflow Feb 26, 2013 by beyonddc

User contributions licensed under CC BY-SA 3.0