我正在尝试使用 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/