本文转自:https://www.cnblogs.com/datacool/p/datacool_poweroff.html
1 /// <summary> 2 /// 计算机电源控制类 3 /// </summary> 4 public class EnvironmentCheckClass 5 { 6 [DllImport("user32.dll")] 7 static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason); 8 [DllImport("kernel32.dll", ExactSpelling = true)] 9 internal static extern IntPtr GetCurrentProcess(); 10 11 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 12 internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok); 13 14 [DllImport("advapi32.dll", SetLastError = true)] 15 internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid); 16 17 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] 18 internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, 19 ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); 20 21 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] 22 internal static extern bool ExitWindowsEx(int flg, int rea); 23 24 25 [StructLayout(LayoutKind.Sequential, Pack = 1)] 26 internal struct TokPriv1Luid 27 { 28 public int Count; 29 public long Luid; 30 public int Attr; 31 } 32 33 internal const int SE_PRIVILEGE_ENABLED = 0x00000002; 34 internal const int TOKEN_QUERY = 0x00000008; 35 internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 36 internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; 37 38 public enum ExitWindows : uint 39 { 40 LogOff = 0x00, //注销 41 ShutDown = 0x01, //关机 42 Reboot = 0x00000002, //重启 43 Force = 0x04, 44 PowerOff = 0x08, 45 ForceIfHung = 0x10 46 } 47 public enum ShutdownReason : uint 48 { 49 MajorApplication = 0x00040000, 50 MajorHardware = 0x00010000, 51 MajorLegacyApi = 0x00070000, 52 MajorOperatingSystem = 0x00020000, 53 MajorOther = 0x00000000, 54 MajorPower = 0x00060000, 55 MajorSoftware = 0x00030000, 56 MajorSystem = 0x00050000, 57 MinorBlueScreen = 0x0000000F, 58 MinorCordUnplugged = 0x0000000b, 59 MinorDisk = 0x00000007, 60 MinorEnvironment = 0x0000000c, 61 MinorHardwareDriver = 0x0000000d, 62 MinorHotfix = 0x00000011, 63 MinorHung = 0x00000005, 64 MinorInstallation = 0x00000002, 65 MinorMaintenance = 0x00000001, 66 MinorMMC = 0x00000019, 67 MinorNetworkConnectivity = 0x00000014, 68 MinorNetworkCard = 0x00000009, 69 MinorOther = 0x00000000, 70 MinorOtherDriver = 0x0000000e, 71 MinorPowerSupply = 0x0000000a, 72 MinorProcessor = 0x00000008, 73 MinorReconfig = 0x00000004, 74 MinorSecurity = 0x00000013, 75 MinorSecurityFix = 0x00000012, 76 MinorSecurityFixUninstall = 0x00000018, 77 MinorServicePack = 0x00000010, 78 MinorServicePackUninstall = 0x00000016, 79 MinorTermSrv = 0x00000020, 80 MinorUnstable = 0x00000006, 81 MinorUpgrade = 0x00000003, 82 MinorWMI = 0x00000015, 83 FlagUserDefined = 0x40000000, 84 FlagPlanned = 0x80000000 85 } 86 87 public void ReStartComputer() 88 { 89 System.Threading.Thread Restart = new System.Threading.Thread(ReStart); 90 Restart.Start(); 91 } 92 93 private void ReStart() 94 { 95 try 96 { 97 System.Threading.Thread.Sleep(1000); 98 bool ok; 99 TokPriv1Luid tp; 100 IntPtr hproc = GetCurrentProcess(); 101 IntPtr htok = IntPtr.Zero; 102 ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); 103 tp.Count = 1; 104 tp.Luid = 0; 105 tp.Attr = SE_PRIVILEGE_ENABLED; 106 ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid); 107 ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); 108 ok = ExitWindowsEx(ExitWindows.Reboot, ShutdownReason.MinorOther); 109 } 110 catch 111 { } 112 } 113 114 public void ComputerPowerOff() 115 { 116 System.Threading.Thread PowerOff = new System.Threading.Thread(PCPowerOff); 117 PowerOff.Start(); 118 } 119 120 private void PCPowerOff() 121 { 122 try 123 { 124 System.Threading.Thread.Sleep(1000); 125 bool ok; 126 TokPriv1Luid tp; 127 IntPtr hproc = GetCurrentProcess(); 128 IntPtr htok = IntPtr.Zero; 129 ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); 130 tp.Count = 1; 131 tp.Luid = 0; 132 tp.Attr = SE_PRIVILEGE_ENABLED; 133 ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid); 134 ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); 135 ok = ExitWindowsEx(ExitWindows.PowerOff, ShutdownReason.MajorHardware); 136 } 137 catch 138 { } 139 } 140 }