Core/Process: Add GetCurrentPid

This commit is contained in:
SirLynix 2024-01-23 10:26:22 +01:00 committed by Jérôme Leclercq
parent 9c102638c0
commit b0648918a7
6 changed files with 25 additions and 5 deletions

View File

@ -29,7 +29,9 @@ namespace Nz
Process& operator=(const Process&) = delete; Process& operator=(const Process&) = delete;
Process& operator=(Process&&) = delete; Process& operator=(Process&&) = delete;
static Result<Pid, std::string> SpawnDetached(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {}); }; static Pid GetCurrentPid();
static Result<Pid, std::string> SpawnDetached(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {});
};
} }
#include <Nazara/Core/Process.inl> #include <Nazara/Core/Process.inl>

View File

@ -15,6 +15,11 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
Pid GetCurrentProcessId()
{
return ::getpid();
}
Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory) Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory)
{ {
struct PidOrErr struct PidOrErr
@ -43,7 +48,8 @@ namespace Nz::PlatformImpl
if (::chdir(workingDirectory.c_str()) != 0) if (::chdir(workingDirectory.c_str()) != 0)
{ {
PidOrErr err; PidOrErr err;
err.pid = grandChildPid; err.pid = -1;
err.err = errno;
pipe.Write(&err, sizeof(err)); pipe.Write(&err, sizeof(err));

View File

@ -12,7 +12,8 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
NAZARA_CORE_API Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {}); Pid GetCurrentProcessId();
Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {});
} }
#endif // NAZARA_CORE_POSIX_PROCESSIMPL_HPP #endif // NAZARA_CORE_POSIX_PROCESSIMPL_HPP

View File

@ -16,6 +16,11 @@
namespace Nz namespace Nz
{ {
Pid Process::GetCurrentPid()
{
return PlatformImpl::GetCurrentProcessId();
}
Result<Pid, std::string> Process::SpawnDetached(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory) Result<Pid, std::string> Process::SpawnDetached(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory)
{ {
return PlatformImpl::SpawnDetachedProcess(program, arguments, workingDirectory); return PlatformImpl::SpawnDetachedProcess(program, arguments, workingDirectory);

View File

@ -85,6 +85,11 @@ namespace Nz::PlatformImpl
return commandLine; return commandLine;
} }
Pid GetCurrentProcessId()
{
return ::GetCurrentProcessId();
}
Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory) Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments, const std::filesystem::path& workingDirectory)
{ {
DWORD creationFlags = CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; DWORD creationFlags = CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS;
@ -100,7 +105,7 @@ namespace Nz::PlatformImpl
}; };
PROCESS_INFORMATION processInfo; PROCESS_INFORMATION processInfo;
BOOL success = CreateProcessW( BOOL success = ::CreateProcessW(
nullptr, // Application name nullptr, // Application name
commandLine.data(), // Command line commandLine.data(), // Command line
nullptr, // Process attributes nullptr, // Process attributes

View File

@ -12,7 +12,8 @@
namespace Nz::PlatformImpl namespace Nz::PlatformImpl
{ {
NAZARA_CORE_API Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {}); Pid GetCurrentProcessId();
Result<Pid, std::string> SpawnDetachedProcess(const std::filesystem::path& program, std::span<const std::string> arguments = {}, const std::filesystem::path& workingDirectory = {});
} }
#endif // NAZARA_CORE_WIN32_PROCESSIMPL_HPP #endif // NAZARA_CORE_WIN32_PROCESSIMPL_HPP