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

Diff of /trunk/NexusPowerCommand/win32api.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/NexusPowerCommand/win32api.cs	2011/10/25 09:59:44	36
+++ trunk/NexusPowerCommand/win32api.cs	2011/10/25 10:16:08	37
@@ -16,48 +16,48 @@
         [DllImport("user32.dll")]
       private  static extern bool ExitWindowsEx(win32api.enums.ExitWindows uFlags, win32api.enums.ShutdownReason dwReason);
 
-        private static void WMIShutdownReboot(win32api.enums.WMIShutdownRebootFlags flags)
-        {
-            if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) // Privilege tobe enabled (here SeShutdownPrivilege)
-            {
-                ManagementScope Scope = null;
-                ConnectionOptions ConnOptions = null;
-                ObjectQuery ObjQuery = null;
-                ManagementObjectSearcher ObjSearcher = null;
-                try
-                {
-                    ConnOptions = new ConnectionOptions();
-                    ConnOptions.Impersonation = ImpersonationLevel.Impersonate;
-                    ConnOptions.EnablePrivileges = true;
-                    Scope = new ManagementScope(@"\ROOT\CIMV2", ConnOptions);
-
-                    Scope.Connect();
-                    ObjQuery = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
-                    ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery);
-                    foreach (ManagementObject operatingSystem in ObjSearcher.Get())
-                    {
-                        //MessageBox.Show("Caption = " + operatingSystem.GetPropertyValue("Caption"));
-                        //MessageBox.Show("Version = " + operatingSystem.GetPropertyValue("Version"));
-                        ManagementBaseObject outParams = operatingSystem.InvokeMethod("Reboot", null, null);
-                    }
-                }
-                catch (Exception ex)
-                {
-                    throw ex;
-                }
-            }
-        }
+        //private static void WMIShutdownReboot(win32api.enums.WMIShutdownRebootFlags flags)
+        //{
+        //    if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) // Privilege tobe enabled (here SeShutdownPrivilege)
+        //    {
+        //        ManagementScope Scope = null;
+        //        ConnectionOptions ConnOptions = null;
+        //        ObjectQuery ObjQuery = null;
+        //        ManagementObjectSearcher ObjSearcher = null;
+        //        try
+        //        {
+        //            ConnOptions = new ConnectionOptions();
+        //            ConnOptions.Impersonation = ImpersonationLevel.Impersonate;
+        //            ConnOptions.EnablePrivileges = true;
+        //            Scope = new ManagementScope(@"\ROOT\CIMV2", ConnOptions);
+
+        //            Scope.Connect();
+        //            ObjQuery = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
+        //            ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery);
+        //            foreach (ManagementObject operatingSystem in ObjSearcher.Get())
+        //            {
+        //                //MessageBox.Show("Caption = " + operatingSystem.GetPropertyValue("Caption"));
+        //                //MessageBox.Show("Version = " + operatingSystem.GetPropertyValue("Version"));
+        //                ManagementBaseObject outParams = operatingSystem.InvokeMethod("Reboot", null, null);
+        //            }
+        //        }
+        //        catch (Exception ex)
+        //        {
+        //            throw ex;
+        //        }
+        //    }
+        //}
 
         #endregion
 
         #region win32api enums
         private class enums
         {
-            internal enum WMIShutdownRebootFlags
-            {
-                Shutdown = 1,
-                Reboot = 2,
-            }
+            //internal enum WMIShutdownRebootFlags
+            //{
+            //    Shutdown = 1,
+            //    Reboot = 2,
+            //}
             [Flags]
             internal enum ExitWindows : uint
             {
@@ -119,10 +119,20 @@
 
 
         #region win32api wrappers
-        public static bool Shutdown() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Shutdown); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
-        public static bool Restart() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Reboot); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
+        //public static bool Shutdown() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Shutdown); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
+        //public static bool Restart() { try { win32api.WMIShutdownReboot(enums.WMIShutdownRebootFlags.Reboot); return true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } }
+        public static bool Shutdown()
+        {
+            try { if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) { bool ret = win32api.ExitWindowsEx(win32api.enums.ExitWindows.ShutDown, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); return ret; } return false; }
+            catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; }
+        }
+        public static bool Restart()
+        {
+            try { if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) { bool ret = win32api.ExitWindowsEx(win32api.enums.ExitWindows.Reboot, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); return ret; } return false; }
+            catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; }
+        }
 
-        public static bool LogOff() { return win32api.ExitWindowsEx((uint)win32api.enums.ExitWindows.LogOff, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); }
+        public static bool LogOff() { return win32api.ExitWindowsEx(win32api.enums.ExitWindows.LogOff, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); }
         public static bool LockScreen() { return win32api.LockWorkStation(); }
         public static bool Sleep() { return Application.SetSuspendState(PowerState.Hibernate, false, false); }
         #endregion

 

  ViewVC Help
Powered by ViewVC 1.1.22