开发者

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

开发者 https://www.devze.com 2023-02-13 10:32 出处:网络
Am trying to convert a VBScript to java using JACOB - Java COM bridge library. \'Create\' method in VBScript accepts a [out] para开发者_开发百科m in it\'s method and it sets it upon method execution

Am trying to convert a VBScript to java using JACOB - Java COM bridge library. 'Create' method in VBScript accepts a [out] para开发者_开发百科m 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.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号