java - 通过JACOB获取java中VBScript(WMI)方法设置的输出参数值

标签 java vbscript wmi jacob

我正在尝试使用 JACOB - Java COM 桥库将 VBScript 转换为 java。 VBScript 中的“创建”方法在其方法中接受 [out] 参数,并在方法执行时设置它,但我不知道如何通过 JACOB 检索它。

有问题的 VBScript:

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 是方法执行后设置的[out] 参数。 (Create API contract)

转换后的java代码(不完整,稍作修改以进行演示):

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?

}

如果您能提供一些指针或相关代码,那就太好了。如果需要的话可以问我更多。提前致谢。

最佳答案

更换

Integer processId = null;

Variant processId = new Variant(0, true);

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

获取它
processId.getIntRef()

关于java - 通过JACOB获取java中VBScript(WMI)方法设置的输出参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129462/

相关文章:

java - jOOQ 将偏移日期时间返回为 Z(UTC),即使它不是

excel - 有什么方法可以加快 VBScript 中的 excel 比较速度?

python - 手动打开 excel 文件允许公式运行,使用 VBScript 或 PowerShell 或 Python 的 win32com 打开 excel 文件则不允许

c# - 确定处理器支持哪些指令

c# - 从他们以管理员身份启动的应用程序登录/模拟为本地/域用户

vbscript - VB 使用 WMI - 获取登录用户

java - 什么调用loadInBackground方法,如何检索带有数据的列表

java - 匹配 Karate 中每个元素的字符串 json 数组响应

[j-*] 的 Java 模式

vbscript - 通过 VBScript 检测耳机是否插入