0

How to detect external process exit in Java using JNA ? My programm exec external aplication1, this application1 launch an other application2 and after exit. Ant my task is get exit code from application2. I can get a pid of child process and try detect it . But waitForSingleObject doesn't work. I think I'm doing something wrong. Help)

     ProcessBuilder processBuilder;
        int pid = 0;
        int pid2 = 0;
        String gpath = "path of application1";
        processBuilder = new ProcessBuilder(gpath);
        processBuilder.directory(new File(gpath.substring(0, gpath.lastIndexOf("\\") + 1)));
        Process process = processBuilder.start();

        if (process.getClass().getName().equals("java.lang.Win32Process") ||
                process.getClass().getName().equals("java.lang.ProcessImpl")) {
            try {
                Field f = process.getClass().getDeclaredField("handle");
                f.setAccessible(true);
                long handl = f.getLong(process);

                Kernel32 kernel = Kernel32.INSTANCE;
                WinNT.HANDLE handle = new WinNT.HANDLE();
                handle.setPointer(Pointer.createConstant(handl));
                pid = kernel.GetProcessId(handle);
                System.out.println("pid "+pid);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();

        WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
        List<Tlhelp32.PROCESSENTRY32> processentry32List = new ArrayList<>();
        try {
            while (kernel32.Process32Next(snapshot, processEntry)) {
                processentry32List.add(processEntry);
            }

        } finally {
            kernel32.CloseHandle(snapshot);
        }


        for (Tlhelp32.PROCESSENTRY32 processentry32 : processentry32List) {
            if (processentry32.th32ParentProcessID.intValue() == pid) {
                pid2 = processentry32.th32ProcessID.intValue();
                System.out.println("pid2 "+pid2);
                break;
            }
        }

        WinNT.HANDLE gameHandle =    Kernel32.INSTANCE.OpenProcess(0x0400,false,pid2);
        Kernel32.INSTANCE.WaitForSingleObject(gameHandle,Kernel32.INFINITE);
if(Kernel32.INSTANCE.GetExitCodeProcess(gameHandle, exitCode)){
System.out.println("application closed without error");
}else{
System.out.println("application closed with error " + Kernel32.INSTANCE.GetLastError()););
}
SDauka
  • 11
  • 1
  • You have a `HANDLE` to the Process [Kernel Object](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724485.aspx) you created. Use one of the [Wait Functions](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687069.aspx) to wait for the process to terminate (a process is signaled when it terminates). – IInspectable Feb 01 '15 at 20:32
  • @IInspectable i used waitForzsingleObject but it doens't work. – SDauka Feb 01 '15 at 20:36
  • 1
    "Doesn't work" is not an error description. – IInspectable Feb 01 '15 at 20:38
  • @IInspectable when i start my program. Program print that game handle is alive and shutdown – SDauka Feb 01 '15 at 20:47
  • I'd recommend using the handle you retrieved from the `process` object above. – IInspectable Feb 01 '15 at 20:57
  • @IInspectable this handle close after start – SDauka Feb 01 '15 at 21:00
  • In that case use the PID you retrieved above. It's not necessary to generate a snapshot. The PID won't change for the lifetime of the process. In your call to `OpenProcess` you have to add the `SYNCHRONIZE` (0x00100000L) flag (see [Process Security and Access Rights](https://msdn.microsoft.com/en-us/windows/desktop/ms684880)). – IInspectable Feb 01 '15 at 21:03
  • Also, checking the return code of the WaitForSingleObject() and calling GetLastError() if necessary would help you determine *why* it "doesn't work". – user2543253 Feb 02 '15 at 17:04
  • @IInspectable Thank you! Now WaitForSingleObject is work. – SDauka Feb 02 '15 at 17:20
  • http://stackoverflow.com/questions/8903510/how-to-get-the-process-output-when-using-jna-and-createprocessw – Rubens Sep 30 '16 at 05:25

0 Answers0