无法从本地系统帐户检索的流程信息

问题描述 投票:0回答:1

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

我在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)。这段代码在用户会话中工作,我得到了结果,但是当从本地系统的窗口服务运行这段代码时,它就失败了。

java winapi jna
1个回答
2
投票

msdn上OpenProcess的文件说:

如果指定的进程是系统进程(0x00000000),则函数将失败,最后一个错误代码为ERROR_INVALID_PARAMETER。

© www.soinside.com 2019 - 2024. All rights reserved.