Getting output parameter value set by VBScript (WMI) method in java via JACOB

2.6k views Asked by At

Am trying to convert a VBScript to java using JACOB - Java COM bridge library. 'Create' method in VBScript accepts a [out] param in it's method and it sets it upon method execution and i couldn't figure out how to retrieve it back via JACOB.

VBScript in question:

Function CreateProcess(strComputer, strCommand)
    Dim objWMIService, objProcess
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\"     & strComputer & "\root\cimv2")
    Set objProcess = objWMIService.Get("Win32_Process")

    errReturn = objProcess.Create (strCommand, Null, Null, intProcessID)

    Set objWMIService = Nothing
    Set objProcess = Nothing

    CreateProcess = intProcessID
End Function

intProcessID is [out] param set after method execution. (Create API contract)

Converted java code(incomplete and modified slightly for demonstration):

public static void createProcess() {
    String host = "localhost";
    String connectStr = String
            .format("winmgmts:{impersonationLevel=impersonate}!\\\\%s\\root\\CIMV2",
                    host);
    ActiveXComponent axWMI = new ActiveXComponent(connectStr);

    Variant vCollection = axWMI.invoke("get", new Variant("Win32_Process"));

    Dispatch d = vCollection.toDispatch();

    Integer processId = null;
    int result = Dispatch.call(d, "Create", "notepad.exe", null, null, processId)
            .toInt();
    System.out.println("Result:" + result);

    // WORKS FINE until here i.e. notepad launches properly, however processId still seems to be null. Following commented code is wrong - doesn't work     

    //Variant v = Dispatch.get(d, "processId"); // even ProcessId doesn't work
    //int pId = v.getInt();
    //System.out.println("process id:"
    //      + pId);

    // what is the right way to get the process ID set by 'Create' method?

}

Would be great if you could provide some pointers or relevant code. Ask me more if needed. Thanks in advance.

1

There are 1 answers

0
mnk On

Replacing

Integer processId = null;

with

Variant processId = new Variant(0, true);

should solve the problem. You should then have process ID of the notepad.exe process in the processId variant, and it can be fetched by

processId.getIntRef()