ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/NexusPowerControl/trunk/NexusPowerCommand/win32api.cs
Revision: 38
Committed: Tue Oct 25 10:20:40 2011 UTC (11 years, 11 months ago) by william
File size: 3896 byte(s)
Log Message:
*** remove old wmi shutdown/reboot code
    +++ using TokenAdjuster.SetPriviledge with SeShutdownPrivilege
	causes shutdown and reboot to work just like logoff (all operations disaply the default 'force/cancel' dialog under windows7/vista

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6 using System.Windows.Forms;
7 using System.Management;
8
9 namespace NexusPowerCommand
10 {
11 public class win32api
12 {
13 #region win32api defined
14 [DllImport("user32.dll")]
15 private static extern bool LockWorkStation();
16 [DllImport("user32.dll")]
17 private static extern bool ExitWindowsEx(win32api.enums.ExitWindows uFlags, win32api.enums.ShutdownReason dwReason);
18 #endregion
19
20 #region win32api enums
21 private class enums
22 {
23 [Flags]
24 internal enum ExitWindows : uint
25 {
26 // ONE of the following five:
27 LogOff = 0x00,
28 ShutDown = 0x01,
29 Reboot = 0x02,
30 PowerOff = 0x08,
31 RestartApps = 0x40,
32 // plus AT MOST ONE of the following two:
33 Force = 0x04,
34 ForceIfHung = 0x10,
35 }
36 [Flags]
37 internal enum ShutdownReason : uint
38 {
39 MajorApplication = 0x00040000,
40 MajorHardware = 0x00010000,
41 MajorLegacyApi = 0x00070000,
42 MajorOperatingSystem = 0x00020000,
43 MajorOther = 0x00000000,
44 MajorPower = 0x00060000,
45 MajorSoftware = 0x00030000,
46 MajorSystem = 0x00050000,
47
48 MinorBlueScreen = 0x0000000F,
49 MinorCordUnplugged = 0x0000000b,
50 MinorDisk = 0x00000007,
51 MinorEnvironment = 0x0000000c,
52 MinorHardwareDriver = 0x0000000d,
53 MinorHotfix = 0x00000011,
54 MinorHung = 0x00000005,
55 MinorInstallation = 0x00000002,
56 MinorMaintenance = 0x00000001,
57 MinorMMC = 0x00000019,
58 MinorNetworkConnectivity = 0x00000014,
59 MinorNetworkCard = 0x00000009,
60 MinorOther = 0x00000000,
61 MinorOtherDriver = 0x0000000e,
62 MinorPowerSupply = 0x0000000a,
63 MinorProcessor = 0x00000008,
64 MinorReconfig = 0x00000004,
65 MinorSecurity = 0x00000013,
66 MinorSecurityFix = 0x00000012,
67 MinorSecurityFixUninstall = 0x00000018,
68 MinorServicePack = 0x00000010,
69 MinorServicePackUninstall = 0x00000016,
70 MinorTermSrv = 0x00000020,
71 MinorUnstable = 0x00000006,
72 MinorUpgrade = 0x00000003,
73 MinorWMI = 0x00000015,
74
75 FlagUserDefined = 0x40000000,
76 FlagPlanned = 0x80000000
77 }
78 }
79 #endregion
80
81 #region win32api wrappers
82 public static bool Shutdown() { if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) { return win32api.ExitWindowsEx(win32api.enums.ExitWindows.ShutDown, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); } else { return false; } }
83 public static bool Restart() { if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) { return win32api.ExitWindowsEx(win32api.enums.ExitWindows.Reboot, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); } else { return false; } }
84 public static bool LogOff() { return win32api.ExitWindowsEx(win32api.enums.ExitWindows.LogOff, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); }
85 public static bool LockScreen() { return win32api.LockWorkStation(); }
86 public static bool Sleep() { return Application.SetSuspendState(PowerState.Hibernate, false, false); }
87 #endregion
88 }
89 }