/[NexusPowerControl]/trunk/NexusPowerCommand/win32api.cs
ViewVC logotype

Contents of /trunk/NexusPowerCommand/win32api.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations) (download)
Tue Oct 25 09:24:35 2011 UTC (8 years ago) by william
File size: 4936 byte(s)
Add: WMI Impersonation
Update Enum:
internal enum WMIShutdownRebootFlags
{
    Shutdown = 1,
    Reboot = 2,
}

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
19 private static void WMIShutdownReboot(win32api.enums.WMIShutdownRebootFlags flags)
20 {
21 ManagementBaseObject mboShutdown = null;
22 ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
23 mcWin32.Get();
24
25 // You can't shutdown without security privileges
26 mcWin32.Scope.Options.EnablePrivileges = true;
27 mcWin32.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
28 ManagementBaseObject mboShutdownParams =
29 mcWin32.GetMethodParameters("Win32Shutdown");
30
31 // Flag 1 means we want to shut down the system. Use "2" to reboot.
32 mboShutdownParams["Flags"] = flags.ToString();
33 mboShutdownParams["Reserved"] = "0";
34 foreach (ManagementObject manObj in mcWin32.GetInstances())
35 {
36 mboShutdown = manObj.InvokeMethod("Win32Shutdown",
37 mboShutdownParams, null);
38 }
39 }
40
41 #endregion
42
43 #region win32api enums
44 private class enums
45 {
46 internal enum WMIShutdownRebootFlags
47 {
48 Shutdown = 1,
49 Reboot = 2,
50 }
51 [Flags]
52 internal enum ExitWindows : uint
53 {
54 // ONE of the following five:
55 LogOff = 0x00,
56 ShutDown = 0x01,
57 Reboot = 0x02,
58 PowerOff = 0x08,
59 RestartApps = 0x40,
60 // plus AT MOST ONE of the following two:
61 Force = 0x04,
62 ForceIfHung = 0x10,
63 }
64
65 [Flags]
66 internal enum ShutdownReason : uint
67 {
68 MajorApplication = 0x00040000,
69 MajorHardware = 0x00010000,
70 MajorLegacyApi = 0x00070000,
71 MajorOperatingSystem = 0x00020000,
72 MajorOther = 0x00000000,
73 MajorPower = 0x00060000,
74 MajorSoftware = 0x00030000,
75 MajorSystem = 0x00050000,
76
77 MinorBlueScreen = 0x0000000F,
78 MinorCordUnplugged = 0x0000000b,
79 MinorDisk = 0x00000007,
80 MinorEnvironment = 0x0000000c,
81 MinorHardwareDriver = 0x0000000d,
82 MinorHotfix = 0x00000011,
83 MinorHung = 0x00000005,
84 MinorInstallation = 0x00000002,
85 MinorMaintenance = 0x00000001,
86 MinorMMC = 0x00000019,
87 MinorNetworkConnectivity = 0x00000014,
88 MinorNetworkCard = 0x00000009,
89 MinorOther = 0x00000000,
90 MinorOtherDriver = 0x0000000e,
91 MinorPowerSupply = 0x0000000a,
92 MinorProcessor = 0x00000008,
93 MinorReconfig = 0x00000004,
94 MinorSecurity = 0x00000013,
95 MinorSecurityFix = 0x00000012,
96 MinorSecurityFixUninstall = 0x00000018,
97 MinorServicePack = 0x00000010,
98 MinorServicePackUninstall = 0x00000016,
99 MinorTermSrv = 0x00000020,
100 MinorUnstable = 0x00000006,
101 MinorUpgrade = 0x00000003,
102 MinorWMI = 0x00000015,
103
104 FlagUserDefined = 0x40000000,
105 FlagPlanned = 0x80000000
106 }
107 }
108 #endregion
109
110
111 #region win32api wrappers
112 public static bool Shutdown() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Shutdown); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
113 public static bool Restart() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Reboot); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
114
115 public static bool LogOff() { return win32api.ExitWindowsEx((uint)win32api.enums.ExitWindows.LogOff, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); }
116 public static bool LockScreen() { return win32api.LockWorkStation(); }
117 public static bool Sleep() { return Application.SetSuspendState(PowerState.Hibernate, false, false); }
118 #endregion
119 }
120 }

  ViewVC Help
Powered by ViewVC 1.1.22