aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-10-16 17:41:19 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-10-16 21:17:58 +0300
commit16fc3e5774069c77ff9ec304555e065ff08e4c6f (patch)
tree0fdf8a23bc691e33775b1db92290b3d2195b6ee9 /src
parentadd a separate Buffer class (diff)
downloadwinapi-common-16fc3e5774069c77ff9ec304555e065ff08e4c6f.tar.gz
winapi-common-16fc3e5774069c77ff9ec304555e065ff08e4c6f.zip
Process: add get_exit_code()
Diffstat (limited to '')
-rw-r--r--src/process.cpp20
1 files changed, 18 insertions, 2 deletions
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