]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/windows/process.cpp
Add move semantics to Variant
[libs/core.git] / source / core / windows / process.cpp
index da76fe38dd3551f82dbfd3910d995a0058191108..0eae33bdab07eb4169d441ff865ac23aadd28b30 100644 (file)
@@ -1,7 +1,8 @@
-#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"
 
@@ -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;
 }
@@ -61,12 +63,12 @@ void Process::execute(const string &command, bool path_search, const Arguments &
 
        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<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)
@@ -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;
 }