Running a Windows program and detect when it ends with C++

前端 未结 7 1666
清酒与你
清酒与你 2020-12-05 21:48

Suppose I run an application, after some time this app will get closed by user. Is it possible to find out when the program exits? Can I get it\'s process id when I run that

7条回答
  •  庸人自扰
    2020-12-05 22:35

    I would like to mention that you don't have to actually create a process, in order to be able to wait for its termination. Basically, if you know its PID, you can do this:

     HANDLE h = OpenProcess(SYNCHRONIZE, TRUE, pid);
     WaitForSingleObject(h, INFINITE );
    

    Full example: https://gist.github.com/rdp/6b5fc8993089ee12b44d (leave a comment here if you'd like a compiled version made available).

提交回复
热议问题