From 16fc3e5774069c77ff9ec304555e065ff08e4c6f Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 16 Oct 2020 17:41:19 +0300 Subject: Process: add get_exit_code() --- src/process.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src') 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 #include +#include #include #include @@ -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(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(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(ec); +} + } // namespace winapi -- cgit v1.2.3