X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fwindows%2Fprocess.cpp;h=3bd03fa3816dbefc3faa71726fa1632c5537cdcd;hb=HEAD;hp=2f7b50547a4e8ff8ef493f58bcbdd90692379933;hpb=ecc3d75ad9567cceee4302445d88c659b41a4899;p=libs%2Fcore.git diff --git a/source/core/windows/process.cpp b/source/core/windows/process.cpp index 2f7b505..0eae33b 100644 --- a/source/core/windows/process.cpp +++ b/source/core/windows/process.cpp @@ -1,7 +1,8 @@ -#include +#include "winapi.h" #include #include #include +#include "except.h" #include "process.h" #include "process_private.h" @@ -14,19 +15,19 @@ string quote_argument(const string &arg) string result; bool need_quotes = false; bool backslash = false; - for(string::const_iterator i=arg.begin(); i!=arg.end(); ++i) + for(char c: arg) { - if(*i=='\\') + if(c=='\\') backslash = true; - else if(*i=='"') + else if(c=='"') { if(backslash) result += '\\'; result += '\\'; } - else if(*i==' ') + else if(c==' ') need_quotes = true; - result += *i; + result += c; } if(need_quotes) @@ -43,12 +44,13 @@ Process::~Process() { CloseHandle(priv->info.hProcess); CloseHandle(priv->info.hThread); + delete priv; } void Process::platform_get_self_info(Private &priv) { priv.info.hProcess = GetCurrentProcess(); - priv.info.hThread = 0; + priv.info.hThread = nullptr; priv.info.dwProcessId = GetCurrentProcessId(); priv.info.dwThreadId = 0; } @@ -56,17 +58,17 @@ void Process::platform_get_self_info(Private &priv) void Process::execute(const string &command, bool path_search, const Arguments &args) { string cmdline = quote_argument(command); - for(Arguments::const_iterator i=args.begin(); i!=args.end(); ++i) - append(cmdline, " ", quote_argument(*i)); + for(const string &a: args) + append(cmdline, " ", quote_argument(a)); STARTUPINFO startup; startup.cb = sizeof(STARTUPINFO); - startup.lpReserved = 0; - startup.lpDesktop = 0; - startup.lpTitle = 0; + startup.lpReserved = nullptr; + startup.lpDesktop = nullptr; + startup.lpTitle = nullptr; startup.dwFlags = 0; startup.cbReserved2 = 0; - startup.lpReserved2 = 0; + startup.lpReserved2 = nullptr; if(redirect) { startup.dwFlags |= STARTF_USESTDHANDLES; @@ -78,9 +80,9 @@ void Process::execute(const string &command, bool path_search, const Arguments & HANDLE cerr_handle = (cerr ? *cerr->get_handle(IO::M_WRITE) : GetStdHandle(STD_ERROR_HANDLE)); DuplicateHandle(self_handle, cerr_handle, self_handle, &startup.hStdError, 0, true, DUPLICATE_SAME_ACCESS); } - const char *cmdptr = (path_search ? 0 : command.c_str()); - const char *wd = (work_dir.empty() ? 0 : work_dir.c_str()); - if(!CreateProcess(cmdptr, const_cast(cmdline.c_str()), 0, 0, true, 0, 0, wd, &startup, &priv->info)) + const char *cmdptr = (path_search ? nullptr : command.c_str()); + const char *wd = (work_dir.empty() ? nullptr : work_dir.c_str()); + if(!CreateProcess(cmdptr, const_cast(cmdline.c_str()), nullptr, nullptr, true, 0, nullptr, wd, &startup, &priv->info)) throw system_error("CreateProcess"); if(redirect) @@ -99,7 +101,7 @@ void Process::execute(const string &command, bool path_search, const Arguments & bool Process::wait(bool block) { if(!running) - throw logic_error("not running"); + throw invalid_state("not running"); DWORD ret = WaitForSingleObject(priv->info.hProcess, (block ? INFINITE : 0)); if(ret==WAIT_FAILED) @@ -109,6 +111,9 @@ bool Process::wait(bool block) { running = false; finished = true; + DWORD ec; + if(GetExitCodeProcess(priv->info.hProcess, &ec)) + exit_code = ec; } return finished; @@ -132,8 +137,8 @@ void Process::interrupt() Process::Private::Private() { - info.hProcess = 0; - info.hThread = 0; + info.hProcess = nullptr; + info.hThread = nullptr; info.dwProcessId = 0; info.dwThreadId = 0; }