aboutsummaryrefslogtreecommitdiffstatshomepage
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
parentadd a separate Buffer class (diff)
downloadwinapi-common-16fc3e5774069c77ff9ec304555e065ff08e4c6f.tar.gz
winapi-common-16fc3e5774069c77ff9ec304555e065ff08e4c6f.zip
Process: add get_exit_code()
-rw-r--r--include/winapi/process.hpp4
-rw-r--r--src/process.cpp20
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