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

前端 未结 7 1676
清酒与你
清酒与你 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:23

    At least on Windows, there is some (underused?) C Runtime Library Functionality available for this. See C Run-Time Library Reference > Process and Environment Control.

    Specifically, you have _spawnlp(_P_WAIT, cmdname, arg0, ..., argn, 0) ("spawn with argument list, using path variable to locate file") which searches cmdname in PATH and in the current directory. Note that "Following argn, there must be a NULL pointer to mark the end of the argument list."

    E.g.

    #include 
    #include 
    int main() {
        puts("waiting");
        _spawnlp(_P_WAIT, "mspaint.exe", "mspaint.exe", 0);
        puts("returned");
    }
    

提交回复
热议问题