Example #1
0
        private static bool EnableShutdown()
        {
            IntPtr           hToken;
            LARGE_INTEGER    luid;
            TOKEN_PRIVILEGES mPriv;

            try
            {
                if (!WinAPI.OpenProcessToken(Process.GetCurrentProcess().Handle, 0x20 | 0x8, out hToken))
                {
                    return(false);
                }
                luid = new LARGE_INTEGER();
                if (WinAPI.LookupPrivilegeValue(null, "SeShutdownPrivilege", luid) != 0)
                {
                    throw new Exception();
                }
                mPriv = new TOKEN_PRIVILEGES();
                mPriv.PrivilegeCount           = 1;
                mPriv.Privileges[0].Attributes = 0x2;
                mPriv.Privileges[0].Luid       = luid;
                if (!WinAPI.AdjustTokenPrivileges(hToken, false, ref mPriv, (uint)(4 +
                                                                                   (12 * mPriv.PrivilegeCount)), IntPtr.Zero, IntPtr.Zero))
                {
                    throw new Exception();
                }
            }
            catch
            {
                GC.Collect();
                return(false);
            }
            GC.Collect();
            return(true);
        }
Example #2
0
 public static extern int LookupPrivilegeValue(string lpSystemName, string lpName,
                                               LARGE_INTEGER lpLuid);