-#include <windows.h>
+#include "winapi.h"
#include <msp/core/systemerror.h>
#include <msp/io/handle_private.h>
#include <msp/strings/utils.h>
+#include "except.h"
#include "process.h"
#include "process_private.h"
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)
{
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;
}
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;
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<char *>(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<char *>(cmdline.c_str()), nullptr, nullptr, true, 0, nullptr, wd, &startup, &priv->info))
throw system_error("CreateProcess");
if(redirect)
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)
{
running = false;
finished = true;
+ DWORD ec;
+ if(GetExitCodeProcess(priv->info.hProcess, &ec))
+ exit_code = ec;
}
return finished;
Process::Private::Private()
{
- info.hProcess = 0;
- info.hThread = 0;
+ info.hProcess = nullptr;
+ info.hThread = nullptr;
info.dwProcessId = 0;
info.dwThreadId = 0;
}