Files
UnleashedRecomp-hedge-dev/UnleashedRecomp/os/win32/process_win32.cpp
Hyper 8b345d2cbd Implemented Windows registry read/write (#225)
* Implemented Windows registry read/write

* Simplify registry API and handle path writes

* Linux SetWorkingDirectory

* Implement reading path and unicode string from windows registry

* Use string_view value names for registry

* Use RegGetValueW

* Paths adjustments

* /

* Update working directory update failure message

* Updated linux SetWorkingDirectory

* Update flatpak define

* Remove RootDirectoryPath and save registry at startup

* dont save registry on exit

* Slight formatting update

---------

Co-authored-by: Sajid <sajidur78@gmail.com>
2025-01-28 14:41:29 +03:00

54 lines
1.4 KiB
C++

#include <os/process.h>
std::filesystem::path os::process::GetExecutablePath()
{
WCHAR exePath[MAX_PATH];
if (!GetModuleFileNameW(nullptr, exePath, MAX_PATH))
return std::filesystem::path();
return std::filesystem::path(exePath);
}
std::filesystem::path os::process::GetWorkingDirectory()
{
WCHAR workPath[MAX_PATH];
if (!GetCurrentDirectoryW(MAX_PATH, workPath))
return std::filesystem::path();
return std::filesystem::path(workPath);
}
bool os::process::SetWorkingDirectory(const std::filesystem::path& path)
{
return SetCurrentDirectoryW(path.c_str());
}
bool os::process::StartProcess(const std::filesystem::path& path, const std::vector<std::string>& args, std::filesystem::path work)
{
if (path.empty())
return false;
if (work.empty())
work = path.parent_path();
auto cli = path.wstring();
// NOTE: We assume the CLI arguments only contain ASCII characters.
for (auto& arg : args)
cli += L" " + std::wstring(arg.begin(), arg.end());
STARTUPINFOW startInfo{ sizeof(STARTUPINFOW) };
PROCESS_INFORMATION procInfo{};
std::wstring pathW = path.wstring();
std::wstring workW = work.wstring();
if (!CreateProcessW(pathW.c_str(), cli.data(), nullptr, nullptr, false, 0, nullptr, workW.c_str(), &startInfo, &procInfo))
return false;
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
return true;
}