In the course of executing your program, you need to start another one and wait for its completion. I do
WinExec("C:\\blabla.exe",1)
but it does not wait. How to?
In the course of executing your program, you need to start another one and wait for its completion. I do
WinExec("C:\\blabla.exe",1)
but it does not wait. How to?
Better yet, use the process creation function.
TCHAR szPath[] = _T("C:\\blabla.exe"); STARTUPINFO si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; memset(&pi, 0, sizeof(pi)); if ( CreateProcess(NULL, szPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) ) { // программа запущена, ждем её завершения DWORD dwWait = WaitForSingleObject(pi.hProcess, INFINITE); if ( dwWait == WAIT_OBJECT_0 ) { // программа благополучно завершилась } else if ( dwWait == WAIT_ABANDONED ) { // программа была насильно "прибита" } // else ну и может быть другие варианты ожидания CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }
Use a couple of calls: ShellExecuteEx(<SHELLEXECUTEINFO>)
to start the process, and WaitForSingleObject(<SHELLEXECUTEINFO>.hProcess, INFINITE)
to wait for it to complete. Example (taken from here and slightly modified):
SHELLEXECUTEINFO ExecInfo; memset(&ExecInfo, 0, sizeof(SHELLEXECUTEINFO)); ExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ExecInfo.lpVerb = _T("open"); ExecInfo.lpFile = L"\\windows\\clog.lnk"; //Запуск процесса if ( ShellExecuteEx(&ExecInfo) ) { //Ожидание завершения процесса WaitForSingleObject(ExecInfo.hProcess, INFINITE); } ...
Simply
system("код команды");
update (see my comment to the answer (?) @zalipuha below)
I did the sh script
(just faster and more visually), but for using system()
it doesn't matter.
avp@avp-ubu1:~/hashcode$ cat tttx.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #define SCRIPT "bla bla.exe" int main (int ac, char *av[]) { FILE *prog = fopen (SCRIPT, "w"); fprintf (prog, "#!/bin/sh\necho А вот и запустили меня ... \\'$0\\'"); fclose(prog); chmod(SCRIPT, 0755); char cmd[1024]; sprintf (cmd, "'./%s'", SCRIPT); system(cmd); return puts("End") == EOF; } avp@avp-ubu1:~/hashcode$ gcc tttx.c avp@avp-ubu1:~/hashcode$ ./a.out А вот и запустили меня ... './bla bla.exe' End avp@avp-ubu1:~/hashcode$
Something like that. In Windows did not check (to study the subtleties cmd reluctance).
system
transmits a const char
that cannot contain a space. Tell me, am I right? It was just in my case that way. I would be very happy if it were not so. UPD @avp, all the same, I came to the solution through ShellExecute
, since it should work out, but the file name parameter there also accepts the data type const char
, but unlike the system
not so capricious and without abuse it accepts string
via .c_str()
. - zalipuhabla
command. Primerchik, how to deal with this in a couple of minutes I will give my answer to the update . - avpSource: https://ru.stackoverflow.com/questions/115028/
All Articles