gpt4 book ai didi

java - 无法从本地系统帐户检索进程信息

转载 作者:行者123 更新时间:2023-12-02 01:38:11 25 4
gpt4 key购买 nike

我正在尝试从计算机获取进程并收集每个进程的相关信息。

我正在 JNA 的帮助下用 Java 编写此功能

public static List<ProcessInfo> getProcessList() throws Exception {
/* Initialize the empty process list. */
List<ProcessInfo> processList = new ArrayList<ProcessInfo>();

/* Create the process snapshot. */
WinNT.HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));

Tlhelp32.PROCESSENTRY32.ByReference pe = new Tlhelp32.PROCESSENTRY32.ByReference();
for (boolean more = Kernel32.INSTANCE.Process32First(snapshot, pe); more; more = Kernel32.INSTANCE.Process32Next(snapshot, pe)) {
/* Open this process; ignore processes that we cannot open. */
WinNT.HANDLE hProcess = Kernel32.INSTANCE.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION, /* PROCESS_QUERY_LIMITED_INFORMATION */false, pe.th32ProcessID.intValue());
if (hProcess == null) {
continue;
}

/* Get the image name. */
char[] imageNameChars = new char[1024];
IntByReference imageNameLen = new IntByReference(imageNameChars.length);

if (!Kernel32.INSTANCE.QueryFullProcessImageName(hProcess, 0, imageNameChars, imageNameLen)) {
throw new Exception("Couldn't get process image name for "
+ pe.th32ProcessID.intValue());
}

/* Add the process info to our list. */
processList.add(new ProcessInfo(pe.th32ProcessID.intValue(), pe.th32ParentProcessID.intValue(), new String(imageNameChars, 0, imageNameLen.getValue())));

/* Close the process handle. */
Kernel32.INSTANCE.CloseHandle(hProcess);
}

/* Close the process snapshot. */
Kernel32.INSTANCE.CloseHandle(snapshot);

/* Return the process list. */
return processList;
}

现在我在 OpenProcess 函数上收到错误 (87)。此代码在用户 session 中工作,我得到了结果,但在从本地系统的窗口服务运行此代码时,它失败了。

最佳答案

OpenProcess的文档msdn 上说:

If the specified process is the System Process (0x00000000), the function fails and the last error code is ERROR_INVALID_PARAMETER.

关于java - 无法从本地系统帐户检索进程信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878512/

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