gpt4 book ai didi

java - jinterop Win32_Process 创建

转载 作者:行者123 更新时间:2023-12-04 05:10:09 25 4
gpt4 key购买 nike

我正在尝试使用 j-interop 实现以下 wmic 命令。

wmic /NODE:192.168.0.195 /USER:Test /PASSWORD:password123 process call create "calc.exe"

我在我的方法中写了这样的代码。我还有另外两种方法可以创建 session 并连接到 WMI 服务,以便处理该部分。
public void wmiExecute() throws JIException {

// Obtain Win32_Process and narrow it as IJIDispatch
Object[] params = new Object[] {
new JIString("Win32_Process"),
new Integer(0),
JIVariant.OPTIONAL_PARAM()
};
JIVariant[] servicesSet = this._wbemServices.callMethodA("InstancesOf", params);
IJIDispatch wbemObjectSet = (IJIDispatch) JIObjectFactory.narrowObject(servicesSet[0].getObjectAsComObject());

params = new Object[] {
"calc.exe",
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM(),
new Integer(0),
};
wbemObjectSet.callMethodA("Create", params);
}

我一直得到一个异常(exception)
Caught Throwable: org.jinterop.dcom.common.JIException: Unknown name. [0x80020006]
org.jinterop.dcom.common.JIException: Unknown name. [0x80020006]

知道有什么问题吗?提前致谢!

最佳答案

这是解决方案...

您不应该使用 InstanceOf 来获取 Win32_Process,因为您只会获取当前正在运行的进程的列表。相反,您应该使用“Get”来获取默认的 Win32_Process。

public void wmiExecute() throws JIException {

// Obtain Win32_Process and narrow it as IJIDispatch
Object[] params = new Object[] {
new JIString("Win32_Process"),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM()
};

// Obtain the default Win32_Process
JIVariant[] service = this._wbemServices.callMethodA("Get", params);

// Convert it to a IJIDispatch object
IJIDispatch wbemObject = (IJIDispatch) JIObjectFactory.narrowObject(service[0].getObjectAsComObject());

// Create input params
Object[] paramsCalc = new Object[] {
new JIString("calc.exe"),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM()
};

// Create the calculator process
JIVariant[] results = wbemObject.callMethodA("Create", paramsCalc);
}

关于java - jinterop Win32_Process 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14991736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com