private static bool GetIsProcessElevated()
        {
            if (SupportUserAccessControl)
            {
                if (!NativeMethods.OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_READ, out IntPtr tokenHandle))
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                NativeMethods.TOKEN_ELEVATION_TYPE elevationResult = NativeMethods.TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault;

                int    elevationResultSize = Marshal.SizeOf((int)elevationResult);
                IntPtr elevationTypePtr    = Marshal.AllocHGlobal(elevationResultSize);

                bool success = NativeMethods.GetTokenInformation(tokenHandle, NativeMethods.TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, (uint)elevationResultSize, out uint _);
                if (success)
                {
                    elevationResult = (NativeMethods.TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr);
                    bool isProcessAdmin = elevationResult == NativeMethods.TOKEN_ELEVATION_TYPE.TokenElevationTypeFull;
                    return(isProcessAdmin);
                }
                else
                {
                    throw new InvalidOperationException(UnableToGetElevationMessage);
                }
            }
            else
            {
                WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                bool             result    = principal.IsInRole(WindowsBuiltInRole.Administrator);
                return(result);
            }
        }
Пример #2
0
        public static bool CheckProcessElevated()
        {
            if (CheckUAC())
            {
                IntPtr tokenHandle = IntPtr.Zero;
                if (!NativeMethods.OpenProcessToken(Process.GetCurrentProcess().Handle, NativeMethods.TOKEN_QUERY, out tokenHandle))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get process token.");
                }

                try
                {
                    uint returnedSize = sizeof(NativeMethods.TOKEN_ELEVATION_TYPE);

                    IntPtr elevationTypePtr = Marshal.AllocHGlobal((int)returnedSize);
                    try
                    {
                        if (NativeMethods.GetTokenInformation(tokenHandle, NativeMethods.TOKEN_INFORMATION_CLASS.TokenElevationType, elevationTypePtr, returnedSize, out returnedSize))
                        {
                            NativeMethods.TOKEN_ELEVATION_TYPE elevationResult = (NativeMethods.TOKEN_ELEVATION_TYPE)Marshal.ReadInt32(elevationTypePtr);
                            switch (elevationResult)
                            {
                            case NativeMethods.TOKEN_ELEVATION_TYPE.TokenElevationTypeDefault:
                                //Token is not split; if user is admin, we're admin.
                                WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                                return(principal.IsInRole(WindowsBuiltInRole.Administrator) || principal.IsInRole(0x200));    //Domain Administrator

                            case NativeMethods.TOKEN_ELEVATION_TYPE.TokenElevationTypeFull:
                                //Token is split, but we're admin.
                                return(true);

                            case NativeMethods.TOKEN_ELEVATION_TYPE.TokenElevationTypeLimited:
                                //Token is split, and we're limited.
                                return(false);

                            default:
                                throw new Exception("Unknown elevation type!");
                            }
                        }
                        else
                        {
                            throw new ApplicationException("Unable to determine the current elevation.");
                        }
                    }
                    finally
                    {
                        if (elevationTypePtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(elevationTypePtr);
                        }
                    }
                }
                finally
                {
                    if (tokenHandle != IntPtr.Zero)
                    {
                        NativeMethods.CloseHandle(tokenHandle);
                    }
                }
            }
            else
            {
                WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                return(principal.IsInRole(WindowsBuiltInRole.Administrator) || principal.IsInRole(0x200)); //Domain Administrator
            }
        }