0

I am trying to get processes from the machine and collect related information for each process.

I am writing this functionality in Java with the help of JNA

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;
    }

Now I am getting the error (87) on OpenProcess function. This code is working from User session and I am getting the result but while running this code from window service of the local system then it is failing.

Akil Vhora
  • 319
  • 1
  • 5
  • 21
  • https://stackoverflow.com/questions/4988082/openprocess-error-87-invalid-parameter – Pateman Feb 26 '19 at 05:35
  • You can only collect information on your own processes this way. You might find [this code](https://github.com/oshi/oshi/blob/master/oshi-core/src/main/java/oshi/software/os/windows/WindowsOperatingSystem.java) useful. – Daniel Widdis Feb 26 '19 at 06:25
  • How do you make sure that you failed? Both 2 ways will get the system process, and `OpenProcess` will fail and return `NULL`, then continue the `for` loop without any error message. So the user session looks seem to work. – Drake Wu Feb 27 '19 at 07:41
  • Also, if some process been killed between `CreateToolhelp32Snapshot` and `OpenProcess`, it will be not able to find the process by id and also return `ERROR_INVALID_PARAMETER` – Drake Wu Feb 27 '19 at 07:44

1 Answers1

2

The document of OpenProcess on msdn says:

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

oWWo
  • 81
  • 5
  • Ok!! but it only gives error while I am running from window service with local system account. If I am running from IDE it works fine. – Akil Vhora Feb 27 '19 at 04:02