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

Contents of /trunk/NexusPowerCommand/win32api.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (show annotations) (download)
Tue Oct 25 09:59:44 2011 UTC (8 years ago) by william
File size: 5471 byte(s)
Obtained TokenAdjuster from: http://www.dotnet247.com/247reference/msgs/58/292150.aspx

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 if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) // Privilege tobe enabled (here SeShutdownPrivilege)
22 {
23 ManagementScope Scope = null;
24 ConnectionOptions ConnOptions = null;
25 ObjectQuery ObjQuery = null;
26 ManagementObjectSearcher ObjSearcher = null;
27 try
28 {
29 ConnOptions = new ConnectionOptions();
30 ConnOptions.Impersonation = ImpersonationLevel.Impersonate;
31 ConnOptions.EnablePrivileges = true;
32 Scope = new ManagementScope(@"\ROOT\CIMV2", ConnOptions);
33
34 Scope.Connect();
35 ObjQuery = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
36 ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery);
37 foreach (ManagementObject operatingSystem in ObjSearcher.Get())
38 {
39 //MessageBox.Show("Caption = " + operatingSystem.GetPropertyValue("Caption"));
40 //MessageBox.Show("Version = " + operatingSystem.GetPropertyValue("Version"));
41 ManagementBaseObject outParams = operatingSystem.InvokeMethod("Reboot", null, null);
42 }
43 }
44 catch (Exception ex)
45 {
46 throw ex;
47 }
48 }
49 }
50
51 #endregion
52
53 #region win32api enums
54 private class enums
55 {
56 internal enum WMIShutdownRebootFlags
57 {
58 Shutdown = 1,
59 Reboot = 2,
60 }
61 [Flags]
62 internal enum ExitWindows : uint
63 {
64 // ONE of the following five:
65 LogOff = 0x00,
66 ShutDown = 0x01,
67 Reboot = 0x02,
68 PowerOff = 0x08,
69 RestartApps = 0x40,
70 // plus AT MOST ONE of the following two:
71 Force = 0x04,
72 ForceIfHung = 0x10,
73 }
74
75 [Flags]
76 internal enum ShutdownReason : uint
77 {
78 MajorApplication = 0x00040000,
79 MajorHardware = 0x00010000,
80 MajorLegacyApi = 0x00070000,
81 MajorOperatingSystem = 0x00020000,
82 MajorOther = 0x00000000,
83 MajorPower = 0x00060000,
84 MajorSoftware = 0x00030000,
85 MajorSystem = 0x00050000,
86
87 MinorBlueScreen = 0x0000000F,
88 MinorCordUnplugged = 0x0000000b,
89 MinorDisk = 0x00000007,
90 MinorEnvironment = 0x0000000c,
91 MinorHardwareDriver = 0x0000000d,
92 MinorHotfix = 0x00000011,
93 MinorHung = 0x00000005,
94 MinorInstallation = 0x00000002,
95 MinorMaintenance = 0x00000001,
96 MinorMMC = 0x00000019,
97 MinorNetworkConnectivity = 0x00000014,
98 MinorNetworkCard = 0x00000009,
99 MinorOther = 0x00000000,
100 MinorOtherDriver = 0x0000000e,
101 MinorPowerSupply = 0x0000000a,
102 MinorProcessor = 0x00000008,
103 MinorReconfig = 0x00000004,
104 MinorSecurity = 0x00000013,
105 MinorSecurityFix = 0x00000012,
106 MinorSecurityFixUninstall = 0x00000018,
107 MinorServicePack = 0x00000010,
108 MinorServicePackUninstall = 0x00000016,
109 MinorTermSrv = 0x00000020,
110 MinorUnstable = 0x00000006,
111 MinorUpgrade = 0x00000003,
112 MinorWMI = 0x00000015,
113
114 FlagUserDefined = 0x40000000,
115 FlagPlanned = 0x80000000
116 }
117 }
118 #endregion
119
120
121 #region win32api wrappers
122 public static bool Shutdown() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Shutdown); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
123 public static bool Restart() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Reboot); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
124
125 public static bool LogOff() { return win32api.ExitWindowsEx((uint)win32api.enums.ExitWindows.LogOff, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); }
126 public static bool LockScreen() { return win32api.LockWorkStation(); }
127 public static bool Sleep() { return Application.SetSuspendState(PowerState.Hibernate, false, false); }
128 #endregion
129 }
130 }

  ViewVC Help
Powered by ViewVC 1.1.22