C#winform program shuts down the correct posture of the computer

Source: Internet
Author: User

    <summary>///Computer Power control class///</summary> public class Environmentcheckclass {[Dllimpo        RT ("User32.dll")] static extern bool ExitWindowsEx (exitwindows uflags, ShutdownReason dwreason);        [DllImport ("kernel32.dll", ExactSpelling = true)] internal static extern IntPtr getcurrentprocess (); [DllImport ("advapi32.dll", ExactSpelling = True, SetLastError = true)] internal static extern bool OpenProcessToken        (IntPtr h, int acc, ref IntPtr Phtok); [DllImport ("advapi32.dll", SetLastError = true)] internal static extern bool Lookupprivilegevalue (string host, Stri        ng name, ref long Pluid); [DllImport ("advapi32.dll", ExactSpelling = True, SetLastError = true)] internal static extern bool Adjusttokenprivi        Leges (IntPtr Htok, bool Disall, ref tokpriv1luid Newst, int len, IntPtr prev, IntPtr Relen); [DllImport ("user32.dll", ExactSpelling = True, SetLastError = true)] internal static extern bool ExitWindowsEx (int flg, int rea);            [StructLayout (layoutkind.sequential, Pack = 1)] Internal struct TOKPRIV1LUID {public int Count;            public long Luid;        public int Attr;        } internal const int se_privilege_enabled = 0x00000002;        Internal const int token_query = 0x00000008;        Internal const int token_adjust_privileges = 0x00000020;        Internal const string se_shutdown_name = "SeShutdownPrivilege";            public enum Exitwindows:uint {LogOff = 0x00,//Logoff ShutDown = 0x01,//shutdown        Reboot = 0x00000002,//Restart force = 0x04, PowerOff = 0x08, Forceifhung = 0x10} public enum Shutdownreason:uint {majorapplication = 0x00040000, Majorhardware = 0x000 10000, Majorlegacyapi = 0x00070000, Majoroperatingsystem = 0x00020000, Majorother = 0x000 00000, Majorpower = 0x00060000, Majorsoftware = 0x00030000, Majorsystem = 0x00050000, Minorbluescreen = 0x 0000000F, minorcordunplugged = 0x0000000b, Minordisk = 0x00000007, minorenvironment = 0x0 000000c, Minorhardwaredriver = 0x0000000d, Minorhotfix = 0x00000011, Minorhung = 0x000000             Minorinstallation = 0x00000002, minormaintenance = 0x00000001, Minormmc = 0x00000019, Minornetworkconnectivity = 0x00000014, Minornetworkcard = 0x00000009, Minorother = 0x000 00000, Minorotherdriver = 0x0000000e, minorpowersupply = 0x0000000a, minorprocessor = 0x0 0000008, Minorreconfig = 0x00000004, minorsecurity = 0x00000013, Minorsecurityfix = 0x000 00012, Minorsecurityfixuninstall = 0x00000018, Minorservicepack = 0x00000010, Minorservic Epackuninstall = 0x00000016, minortermsrv = 0x00000020, minorunstable = 0x00000006, Minorupgrade = 0x00000003,        MINORWMI = 0x00000015, flaguserdefined = 0x40000000, flagplanned = 0x80000000} public void Restartcomputer () {System.Threading.Thread Restart = new System.Threading.Thread (Restart            );        Restart.start ();                 } private void ReStart () {try {System.Threading.Thread.Sleep (1000);                BOOL OK;                TOKPRIV1LUID TP;                IntPtr hproc = GetCurrentProcess ();                IntPtr Htok = IntPtr.Zero; OK = OpenProcessToken (hproc, Token_adjust_privileges |                Token_query, ref Htok); Tp.                Count = 1; Tp.                Luid = 0; Tp.                Attr = se_privilege_enabled; OK = Lookupprivilegevalue (null, SE_SHUTDOWN_NAME, ref TP.                LUID); OK = adjusttokenprivileges (Htok,False, ref TP, 0, IntPtr.Zero, IntPtr.Zero);            OK = ExitWindowsEx (exitwindows.reboot, Shutdownreason.minorother); } catch {}} public void Computerpoweroff () {System.Threading.Thre            Ad PowerOff = new System.Threading.Thread (Pcpoweroff);        Poweroff.start (); } private void Pcpoweroff () {try {System.Threading.Thread.Sleep (1000)                  ;                BOOL OK;                TOKPRIV1LUID TP;                IntPtr hproc = GetCurrentProcess ();                IntPtr Htok = IntPtr.Zero; OK = OpenProcessToken (hproc, Token_adjust_privileges |                Token_query, ref Htok); Tp.                Count = 1; Tp.                Luid = 0; Tp.                Attr = se_privilege_enabled; OK = Lookupprivilegevalue (null, SE_SHUTDOWN_NAME, ref TP.                LUID);     OK = AdjustTokenPrivileges (Htok, FALSE, ref TP, 0, IntPtr.Zero, IntPtr.Zero);           OK = ExitWindowsEx (Exitwindows.poweroff, Shutdownreason.majorhardware); } Catch {}}}

  

C#winform program shuts down the correct posture of the computer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.