2

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.

4

1 回答 1

4

更换

Integer processId = null;

Variant processId = new Variant(0, true);

应该解决问题。然后,您应该在 processId 变体中拥有 notepad.exe 进程的进程 ID,并且可以通过以下方式获取它

processId.getIntRef()
于 2011-05-24T13:24:33.780 回答