Support fetching the windows username

This commit is contained in:
Maurice Heumann 2023-03-07 18:33:39 +01:00
parent dbee7d9741
commit 73f9859eed
2 changed files with 21 additions and 3 deletions

View File

@ -1,4 +1,7 @@
#include "nt.hpp" #include "nt.hpp"
#include <lmcons.h>
#include "string.hpp" #include "string.hpp"
namespace utils::nt namespace utils::nt
@ -234,14 +237,14 @@ namespace utils::nt
{ {
registry_key new_key{}; registry_key new_key{};
if (RegOpenKeyExA(current_key, part.data(), 0, if (RegOpenKeyExA(current_key, part.data(), 0,
KEY_ALL_ACCESS, &new_key) == ERROR_SUCCESS) KEY_ALL_ACCESS, &new_key) == ERROR_SUCCESS)
{ {
current_key = std::move(new_key); current_key = std::move(new_key);
continue; continue;
} }
if (RegCreateKeyExA(current_key, part.data(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, if (RegCreateKeyExA(current_key, part.data(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
nullptr, &new_key, nullptr) != ERROR_SUCCESS) nullptr, &new_key, nullptr) != ERROR_SUCCESS)
{ {
return {}; return {};
} }
@ -310,7 +313,8 @@ namespace utils::nt
GetCurrentDirectoryA(sizeof(current_dir), current_dir); GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA(); auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().generic_string().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir, CreateProcessA(self.get_path().generic_string().data(), command_line, nullptr, nullptr, false, NULL, nullptr,
current_dir,
&startup_info, &process_info); &startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread); if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread);
@ -322,4 +326,16 @@ namespace utils::nt
TerminateProcess(GetCurrentProcess(), code); TerminateProcess(GetCurrentProcess(), code);
_Exit(code); _Exit(code);
} }
std::string get_user_name()
{
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
if (!GetUserNameA(username, &username_len))
{
return {};
}
return std::string(username, username_len - 1);
}
} }

View File

@ -252,4 +252,6 @@ namespace utils::nt
void relaunch_self(); void relaunch_self();
__declspec(noreturn) void terminate(uint32_t code = 0); __declspec(noreturn) void terminate(uint32_t code = 0);
std::string get_user_name();
} }