From 06317d63e7c7a6da42d6f2b28f45f9862b74a6b0 Mon Sep 17 00:00:00 2001 From: icex2 Date: Sun, 28 Jan 2024 21:58:24 +0100 Subject: [PATCH] feat(util): Add func to check if running as admin user This has been a source of common error in the past. It is known that most, or even all, games run into various issues when not run with elevated privileges. --- src/main/util/proc.c | 50 +++++++++++++++++++++++++++++++++++++++++++- src/main/util/proc.h | 3 +++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/util/proc.c b/src/main/util/proc.c index 19cec50..8473113 100644 --- a/src/main/util/proc.c +++ b/src/main/util/proc.c @@ -1,7 +1,55 @@ -#include +#define LOG_MODULE "util-proc" +#include +#include + +#include #include +#include "util/log.h" + +bool proc_is_running_as_admin_user() +{ + SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY; + PSID sid; + BOOL res; + BOOL is_admin; + + res = AllocateAndInitializeSid( + &authority, + 2, + SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, + 0, + 0, + 0, + 0, + 0, + 0, + &sid); + + if (!res) { + log_warning( + "Failed to allocate memory for is admin check: %lX", + GetLastError()); + return false; + } + + is_admin = false; + + res = CheckTokenMembership(NULL, sid, &is_admin); + + if (!res) { + log_warning( + "Failed to check admin group membership: %lX", GetLastError()); + return false; + } + + FreeSid(sid); + + return is_admin; +} + void proc_terminate_current_process(uint32_t exit_code) { HANDLE hnd; diff --git a/src/main/util/proc.h b/src/main/util/proc.h index 3dc6906..be47a40 100644 --- a/src/main/util/proc.h +++ b/src/main/util/proc.h @@ -1,5 +1,8 @@ #pragma once +#include #include +bool proc_is_running_as_admin_user(); + void proc_terminate_current_process(uint32_t exit_code); \ No newline at end of file