internal static extern bool AdjustTokenPrivileges()

in Managed/NativeMethods/AdvApi32.cs [262:361]


        internal static extern bool AdjustTokenPrivileges(
            SafeHandleZeroIsInvalid TokenHandle,
            [MarshalAs(UnmanagedType.Bool)]
            bool DisableAllPrivileges,
            ref TOKEN_PRIVILEGES NewState,
            int len,
            IntPtr prev,
            IntPtr relen);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetTokenInformation(
            SafeHandleZeroIsInvalid TokenHandle,
            TOKEN_INFORMATION_CLASS TokenInformationClass,
            HGlobalBuffer TokenInformation,
            int TokenInformationLength,
            out int ReturnLength);

        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool OpenProcessToken(
            SafeHandleZeroIsInvalid ProcessHandle,
            AccessTokenRights DesiredAccess,
            out SafeHandleZeroIsInvalid TokenHandle);

        [DllImport("ADVAPI32.DLL"),
         SuppressUnmanagedCodeSecurity,
         ResourceExposure(ResourceScope.None),
         ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        internal static extern int RegCloseKey(IntPtr hKey);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegOpenKeyEx(SafeRegistryHandle hKey, String lpSubKey,
                    int ulOptions, int samDesired, out SafeRegistryHandle hkResult);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, String lpValueName,
                    int[] lpReserved, ref int lpType, [Out] byte[] lpData,
                    ref int lpcbData);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, String lpValueName,
                    int[] lpReserved, ref int lpType, ref int lpData,
                    ref int lpcbData);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, String lpValueName,
                    int[] lpReserved, ref int lpType, ref long lpData,
                    ref int lpcbData);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, String lpValueName,
                     int[] lpReserved, ref int lpType, [Out] char[] lpData,
                     ref int lpcbData);

        [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)]
        internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, String lpValueName,
                    int[] lpReserved, ref int lpType, StringBuilder lpData,
                    ref int lpcbData);

        public static void EnableShutdownPrivilege()
        {
            const int SE_PRIVILEGE_ENABLED = 0x00000002;
            const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

            bool retVal;

            using (SafeHandleZeroIsInvalid hproc = Kernel32.NativeMethods.GetCurrentProcess())
            {
                TOKEN_PRIVILEGES tp;
                SafeHandleZeroIsInvalid htok;
                retVal = OpenProcessToken(hproc, AccessTokenRights.TOKEN_ADJUST_PRIVILEGES | AccessTokenRights.TOKEN_QUERY, out htok);
                if (!retVal)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                using (htok)
                {
                    tp.PrivilegeCount = 1;
                    tp.Luid = 0;
                    tp.Attributes = SE_PRIVILEGE_ENABLED;
                    retVal = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
                    if (!retVal)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
                    if (!retVal)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                }
            }
        }