aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/process.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-10-25 01:57:29 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-10-25 01:57:29 +0300
commit1abf2fbac63c90c315cf6e87d6bce0c285bd4019 (patch)
tree0af5adb917b99d762f75c0407f0f018ecaf72a3e /src/process.cpp
parentremove redundant namespace names (diff)
downloadwinapi-common-1abf2fbac63c90c315cf6e87d6bce0c285bd4019.tar.gz
winapi-common-1abf2fbac63c90c315cf6e87d6bce0c285bd4019.zip
Process: add termination methods
Diffstat (limited to 'src/process.cpp')
-rw-r--r--src/process.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/process.cpp b/src/process.cpp
index a9f24cc..c5e68d7 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -110,6 +110,22 @@ Process Process::runas(const CommandLine& cmd_line) {
return Process{shell_execute(cmd_line)};
}
+bool Process::is_running() const {
+ const auto ret = ::WaitForSingleObject(static_cast<HANDLE>(m_handle), 0);
+
+ switch (ret) {
+ case WAIT_OBJECT_0:
+ return false;
+ case WAIT_TIMEOUT:
+ return true;
+ case WAIT_FAILED:
+ throw error::windows(GetLastError(), "WaitForSingleObject");
+ default:
+ // Shouldn't happen.
+ throw error::custom(ret, "WaitForSingleObject");
+ }
+}
+
void Process::wait() const {
const auto ret = ::WaitForSingleObject(static_cast<HANDLE>(m_handle), INFINITE);
@@ -124,6 +140,17 @@ void Process::wait() const {
}
}
+void Process::terminate(int ec) const {
+ if (!::TerminateProcess(static_cast<HANDLE>(m_handle), static_cast<UINT>(ec))) {
+ throw error::windows(GetLastError(), "TerminateProcess");
+ }
+}
+
+void Process::shut_down(int ec) const {
+ terminate(ec);
+ wait();
+}
+
int Process::get_exit_code() const {
DWORD ec = 0;