【发布时间】:2011-02-26 20:12:50
【问题描述】:
我正在尝试使用 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?
}
如果您能提供一些指针或相关代码,那就太好了。如果需要,请向我询问更多。提前致谢。
【问题讨论】:
-
我对 JACOB 一无所知,但你能把你的 vbscript 放到 Windows 脚本组件 (WSC) 中吗? wsc 将 vbscript 包装成可以充当 com 库本身的东西 - 在 mdsn 上搜索。