From dabba4b1e7b567e768204a7665bc75ade586a391 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 9 Aug 2021 21:07:25 +0200 Subject: Added ProcessHandle::Reset and added some diagnostics for ProcessHandle::Initialize for the case when OpenProcess fails --- zencore/thread.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 9445682be..fa9da0258 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -151,11 +152,7 @@ ProcessHandle::Initialize(void* ProcessHandle) ProcessHandle::~ProcessHandle() { - if (IsValid()) - { - CloseHandle(m_ProcessHandle); - m_ProcessHandle = nullptr; - } + Reset(); } void @@ -163,7 +160,15 @@ ProcessHandle::Initialize(int Pid) { ZEN_ASSERT(m_ProcessHandle == nullptr); m_ProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, Pid); - m_Pid = Pid; + + using namespace fmt::literals; + + if (!m_ProcessHandle) + { + ThrowLastError("ProcessHandle::Initialize(pid: {}) failed"_format(Pid)); + } + + m_Pid = Pid; } bool @@ -197,6 +202,16 @@ ProcessHandle::Terminate(int ExitCode) } } +void +ProcessHandle::Reset() +{ + if (IsValid()) + { + CloseHandle(m_ProcessHandle); + m_ProcessHandle = nullptr; + } +} + bool ProcessHandle::Wait(int TimeoutMs) { -- cgit v1.2.3