Core/Process: Add Exists function

This commit is contained in:
SirLynix
2024-01-23 11:35:35 +01:00
committed by Jérôme Leclercq
parent b0648918a7
commit b63c9fcc49
6 changed files with 44 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/Core/Win32/Win32Utils.hpp>
#include <NazaraUtils/CallOnExit.hpp>
#include <Windows.h>
#include <Nazara/Core/Debug.hpp>
@@ -85,6 +86,27 @@ namespace Nz::PlatformImpl
return commandLine;
}
Result<bool, std::string> CheckProcessExistence(Pid pid)
{
HANDLE processHandle = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
if (!processHandle)
{
DWORD lastErr = ::GetLastError();
if (lastErr == ERROR_INVALID_PARAMETER)
return Ok(false);
return Err("failed to open process handle: " + Error::GetLastSystemError());
}
CallOnExit releaseHandle([processHandle] { ::CloseHandle(processHandle); });
// Opening a handle doesn't mean the process hasn't exited (zombie processes), check if it has an exit code
DWORD exitCode;
if (!::GetExitCodeProcess(processHandle, &exitCode))
return Err("failed to get exit code: " + Error::GetLastSystemError());
return Ok(exitCode == STILL_ACTIVE);
}
Pid GetCurrentProcessId()
{
return ::GetCurrentProcessId();

View File

@@ -12,6 +12,7 @@
namespace Nz::PlatformImpl
{
Result<bool, std::string> CheckProcessExistence(Pid pid);
Pid GetCurrentProcessId();
Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {});
}