C++'s “system” without wait (Win32)

时光毁灭记忆、已成空白 提交于 2019-12-01 10:45:28

Use CreateProcess(), it runs asynchronously. Then you would only have to ensure that updater.exe can write to the original EXE, which you can do by waiting or retrying until the original process has ended. (With a grace interval of course.)

There is no fork() in Win32. The API call you are looking for is called ::CreateProcess(). This is the underlying function that system() is using. ::CreateProcess() is inherently asynchronous: unless you are specifically waiting on the returned process handle, the call is non-blocking.

There is also a higher-level function ::ShellExecute(), that you could use if you are not redirecting process standard I/O or doing the waiting on the process. This has an advantage of searching the system PATH for the executable file, as well as the ability to launch batch files and even starting a program associated with a document file.

You need a thread for that Look here: http://msdn.microsoft.com/en-us/library/y6h8hye8(v=vs.80).aspx You are currently writing your code in the "main thread" (which usually is also your frame code). So if you run something that takes time to complete it will halt the execution of your main thread, if you run it in a second thread your main thread will continue.

Update: I've missed the part that you want to exit immediately. execl() is likely what you want.

#include <unistd.h>

int main(){

    execl("C:\\path\\to\\updater.exe", (const char *) 0);
    return 0;
}

The suggested CreateProcess() can be used as well but execl is conforming to POSIX and would keep your code more portable (if you care at all).

#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);

Update: tested on Win-7 using gcc as compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!