Skip to content

Portable Tool for an Ideal Windows Setup

fix startup check allowing the utility to run without administrator privileges

Greedeks5fbaacdb4ba6
diff --git a/.Source/GTweak/Modules/Common/RunGuard.cs b/.Source/GTweak/Modules/Common/RunGuard.cs
index e07c9da8..d059e6cb 100644
--- a/.Source/GTweak/Modules/Common/RunGuard.cs
+++ b/.Source/GTweak/Modules/Common/RunGuard.cs
@@ -2,7 +2,6 @@ using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
-using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
@@ -14,8 +13,21 @@ namespace GTweak.Modules.Common
{
internal static class RunGuard
{
+ [DllImport("kernel32.dll")]
+ private static extern IntPtr GetCurrentProcess();
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ private static extern bool CloseHandle(IntPtr handle);
+
+ [DllImport("advapi32.dll", SetLastError = true)]
+ private static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
+
+ [DllImport("advapi32.dll", SetLastError = true)]
+ private static extern bool GetTokenInformation(IntPtr tokenHandle, int tokenInfoClass, ref int tokenInformation, int tokenInfoLength, out int returnLength);
+
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr handle, int cmdShow);
+
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr handle);
@@ -59,15 +71,29 @@ namespace GTweak.Modules.Common
internal static void CheckingAdministratorPrivileges()
{
- using WindowsIdentity identity = WindowsIdentity.GetCurrent();
-
- if (new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator))
+ IntPtr token = IntPtr.Zero;
+ try
{
- return;
+ if (OpenProcessToken(GetCurrentProcess(), 0x0008, out token))
+ {
+ int elevation = 0;
+ if (GetTokenInformation(token, 20, ref elevation, sizeof(int), out _) && elevation != 0)
+ {
+ return;
+ }
+ }
+ }
+ finally
+ {
+ if (token != IntPtr.Zero)
+ {
+ CloseHandle(token);
+ }
}
new MessageWindow(MessageWindow.MessageWindowType.NotAdmin).ShowDialog();
}
+
internal static void CheckingDefenderExclusions() => CommandExecutor.RunCommandAsTrustedInstaller($@"$ErrorActionPreference = 'Stop'; $target = '{GlobalOptions.CurrentLocation}'; try {{ $mp = Get-MpPreference; if ($mp.ExclusionProcess -notcontains $target) {{ Add-MpPreference -ExclusionProcess $target }}; if ($mp.ExclusionPath -notcontains $target) {{ Add-MpPreference -ExclusionPath $target }} }} catch {{}}", true);
}
}
diff --git a/.Source/GTweak/Modules/Helpers/TakingOwnership.cs b/.Source/GTweak/Modules/Helpers/TakingOwnership.cs
index d3898b11..ffe53347 100644
--- a/.Source/GTweak/Modules/Helpers/TakingOwnership.cs
+++ b/.Source/GTweak/Modules/Helpers/TakingOwnership.cs
@@ -106,8 +106,7 @@ namespace GTweak.Modules.Helpers
}
private const string SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege";
- private static SID_IDENTIFIER_AUTHORITY SECURITY_NT_AUTHORITY =
- new SID_IDENTIFIER_AUTHORITY() { Value = new byte[] { 0, 0, 0, 0, 0, 5 } };
+ private static SID_IDENTIFIER_AUTHORITY SECURITY_NT_AUTHORITY = new SID_IDENTIFIER_AUTHORITY() { Value = new byte[] { 0, 0, 0, 0, 0, 5 } };
private const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
private const int NO_INHERITANCE = 0x0;
@@ -156,31 +155,13 @@ namespace GTweak.Modules.Helpers
[DllImport("advapi32.dll", EntryPoint = "AdjustTokenPrivileges")]
[return: MarshalAs(UnmanagedType.Bool)]
- private static extern bool AdjustTokenPrivileges([In()] IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges,
- [In()]
- ref TOKEN_PRIVILEGES NewState,
- uint BufferLength,
- IntPtr PreviousState,
- IntPtr ReturnLength);
+ private static extern bool AdjustTokenPrivileges([In()] IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, [In()] ref TOKEN_PRIVILEGES NewState, uint BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
- private static extern int SetNamedSecurityInfo(
- string pObjectName,
- SE_OBJECT_TYPE ObjectType,
- SECURITY_INFORMATION SecurityInfo,
- IntPtr psidOwner,
- IntPtr psidGroup,
- IntPtr pDacl,
- IntPtr pSacl);
-
- [DllImport("advapi32.dll", EntryPoint = "SetEntriesInAclA",
- CallingConvention = CallingConvention.Winapi,
- SetLastError = true, CharSet = CharSet.Ansi)]
- private static extern int SetEntriesInAcl(
- int CountofExplicitEntries,
- ref EXPLICIT_ACCESS ea,
- IntPtr OldAcl,
- ref IntPtr NewAcl);
+ private static extern int SetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, IntPtr psidOwner, IntPtr psidGroup, IntPtr pDacl, IntPtr pSacl);
+
+ [DllImport("advapi32.dll", EntryPoint = "SetEntriesInAclA", CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Ansi)]
+ private static extern int SetEntriesInAcl(int CountofExplicitEntries, ref EXPLICIT_ACCESS ea, IntPtr OldAcl, ref IntPtr NewAcl);
internal static void GrantAdministratorsAccess(string name, SE_OBJECT_TYPE type)
{