diff options
Diffstat (limited to '')
-rw-r--r-- | include/winapi/process.hpp | 4 | ||||
-rw-r--r-- | src/process.cpp | 20 |
2 files changed, 21 insertions, 3 deletions
diff --git a/include/winapi/process.hpp b/include/winapi/process.hpp index 2614c0f..f404b33 100644 --- a/include/winapi/process.hpp +++ b/include/winapi/process.hpp @@ -48,7 +48,9 @@ public: static Process create(const CommandLine&); static Process create(const CommandLine&, IO); - void wait(); + void wait() const; + + int get_exit_code() const; private: explicit Process(Handle&& handle) : m_handle{std::move(handle)} {} diff --git a/src/process.cpp b/src/process.cpp index 5887b05..508f9e1 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -13,6 +13,7 @@ #include <windows.h> #include <cstring> +#include <stdexcept> #include <utility> #include <vector> @@ -79,12 +80,11 @@ Process Process::create(const CommandLine& cmd_line, IO io) { return Process{create_process(cmd_line, io)}; } -void Process::wait() { +void Process::wait() const { const auto ret = ::WaitForSingleObject(static_cast<HANDLE>(m_handle), INFINITE); switch (ret) { case WAIT_OBJECT_0: - m_handle.close(); return; case WAIT_FAILED: throw error::windows(GetLastError(), "WaitForSingleObject"); @@ -94,4 +94,20 @@ void Process::wait() { } } +int Process::get_exit_code() const { + DWORD ec = 0; + + const auto ret = ::GetExitCodeProcess(static_cast<HANDLE>(m_handle), &ec); + + if (!ret) { + throw error::windows(GetLastError(), "GetExitCodeProcess"); + } + + if (ec == STILL_ACTIVE) { + throw std::runtime_error{"Attempted to query the exit code of a running process"}; + } + + return static_cast<int>(ec); +} + } // namespace winapi |