From 51474b2eaef730f49ff810c346f5502ba47e1ae4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 13 Dec 2021 22:58:23 +0100 Subject: [PATCH] ui: Added icon to footer when ImHex has elevated permissions --- plugins/builtin/source/content/ui_items.cpp | 6 ++++ .../libimhex/include/hex/helpers/utils.hpp | 2 ++ plugins/libimhex/source/helpers/utils.cpp | 29 +++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 0def86013..252913673 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -13,6 +13,12 @@ namespace hex::plugin::builtin { void addFooterItems() { + if (hex::isProcessElevated()) { + ContentRegistry::Interface::addFooterItem([] { + ImGui::TextUnformatted(ICON_VS_SHIELD); + }); + } + ContentRegistry::Interface::addFooterItem([] { static float framerate = 0; if (ImGui::HasSecondPassed()) { diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 3131b1848..3dab941c4 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -237,6 +237,8 @@ namespace hex { return *value; } + bool isProcessElevated(); + namespace scope_guard { #define SCOPE_GUARD ::hex::scope_guard::ScopeGuardOnExit() + [&]() diff --git a/plugins/libimhex/source/helpers/utils.cpp b/plugins/libimhex/source/helpers/utils.cpp index 84cb2bedf..fc424a249 100644 --- a/plugins/libimhex/source/helpers/utils.cpp +++ b/plugins/libimhex/source/helpers/utils.cpp @@ -7,9 +7,11 @@ #include -#if defined(OS_WINDOWS) +#if defined (OS_WINDOWS) #include -#elif defined(OS_MACOS) +#elif defined (OS_LINUX) + #include +#elif defined (OS_MACOS) #include #include #endif @@ -253,4 +255,27 @@ namespace hex { return reinterpret_cast(result); } + bool isProcessElevated() { + #if defined (OS_WINDOWS) + bool elevated = false; + HANDLE token = INVALID_HANDLE_VALUE; + + if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) { + TOKEN_ELEVATION elevation; + DWORD elevationSize = sizeof(TOKEN_ELEVATION); + + if (::GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &elevationSize)) + elevated = elevation.TokenIsElevated; + } + + if (token != INVALID_HANDLE_VALUE) + ::CloseHandle(token); + + return elevated; + + #elif defined(OS_LINUX) || defined (OS_MACOS) + return getuid() < 0 || getuid() != geteuid(); + #endif + } + } \ No newline at end of file