If you want to call external process and want to wait for it to terminate, you can call CreateProcess function and wait using WaitForSingeObject API. However, some process just launches child process and terminates immediately, resulting application still running but process terminates and returns to code. Below is sample code you can use in such case, where it monitors for process and child processes, and wait for all family process to terminate.
[C++]Run command within program without showing window / Takeown example
When developing simple tool or doing automation, calling shell command might be easy and fast. However, calling “cmd /c” with ShellExecute brings up shell window, and might return to code before command completes. In such case, use CreateProcess to call cmd. You can hide shell window and wait for process to complete, and even get return value by using CreateProcess API. Below is simple sample code for calling takeown command, which is a command to take ownership of system files which can’t be modified even with Administrator privilege.