rawfilewizard

git clone https://git.clttr.info/rawfilewizard.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

UacHelper.cs (4347B)


      1 using Microsoft.Win32;
      2 using System;
      3 using System.Diagnostics;
      4 using System.Runtime.InteropServices;
      5 using System.Security.Principal;
      6 
      7 namespace JpegDivider
      8 {
      9     public static class UacHelper
     10     {
     11         private const string uacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
     12         private const string uacRegistryValue = "EnableLUA";
     13 
     14         private static uint STANDARD_RIGHTS_READ = 0x00020000;
     15         private static uint TOKEN_QUERY = 0x0008;
     16         private static uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY);
     17 
     18         [DllImport("advapi32.dll", SetLastError = true)]
     19         [return: MarshalAs(UnmanagedType.Bool)]
     20         static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
     21 
     22         [DllImport("advapi32.dll", SetLastError = true)]
     23         public static extern bool GetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, uint TokenInformationLength, out uint ReturnLength);
     24 
     25         public enum TOKEN_INFORMATION_CLASS
     26         {
     27             TokenUser = 1,
     28             TokenGroups,
     29             TokenPrivileges,
     30             TokenOwner,
     31             TokenPrimaryGroup,
     32             TokenDefaultDacl,
     33             TokenSource,
     34             TokenType,
     35             TokenImpersonationLevel,
     36             TokenStatistics,
     37             TokenRestrictedSids,
     38             TokenSessionId,
     39             TokenGroupsAndPrivileges,
     40             TokenSessionReference,
     41             TokenSandBoxInert,
     42             TokenAuditPolicy,
     43             TokenOrigin,
     44             TokenElevationType,
     45             TokenLinkedToken,
     46             TokenElevation,
     47             TokenHasRestrictions,
     48             TokenAccessInformation,
     49             TokenVirtualizationAllowed,
     50             TokenVirtualizationEnabled,
     51             TokenIntegrityLevel,
     52             TokenUIAccess,
     53             TokenMandatoryPolicy,
     54             TokenLogonSid,
     55             MaxTokenInfoClass
     56         }
     57 
     58         public enum TOKEN_ELEVATION_TYPE
     59         {
     60             TokenElevationTypeDefault = 1,
     61             TokenElevationTypeFull,
     62             TokenElevationTypeLimited
     63         }
     64 
     65         public static bool IsUacEnabled
     66         {
     67             get
     68             {
     69                 RegistryKey uacKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, false);
     70                 bool result = uacKey.GetValue(uacRegistryValue).Equals(1);
     71                 return result;
     72             }
     73         }
     74 
     75         public static bool IsProcessElevated
     76         {
     77             get
     78             {
     79                 if (IsUacEnabled)
     80                 {
     81                     IntPtr tokenHandle;
     82                     if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, out tokenHandle))
     83                     {
     84                         throw new ApplicationException("Could not get process token.  Win32 Error Code: " + Marshal.GetLastWin32Error());
     85                     }
     86 
     87                     TOKEN_ELEVATION_TYPE elevationResult = TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault;
     88 
     89                     int elevationResultSize = Marshal.SizeOf((int)elevationResult);
     90                     uint returnedSize = 0;
     91                     IntPtr elevationTypePtr = Marshal.AllocHGlobal(elevationResultSize);
     92 
     93                     bool success = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, (uint)elevationResultSize, out returnedSize);
     94                     if (success)
     95                     {
     96                         elevationResult = (TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr);
     97                         bool isProcessAdmin = elevationResult == TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
     98                         return isProcessAdmin;
     99                     }
    100                     else
    101                     {
    102                         throw new ApplicationException("Unable to determine the current elevation.");
    103                     }
    104                 }
    105                 else
    106                 {
    107                     WindowsIdentity identity = WindowsIdentity.GetCurrent();
    108                     WindowsPrincipal principal = new WindowsPrincipal(identity);
    109                     bool result = principal.IsInRole(WindowsBuiltInRole.Administrator);
    110                     return result;
    111                 }
    112             }
    113         }
    114     }
    115 }