/[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 10:16:08	37
+++ trunk/NexusPowerCommand/win32api.cs	2011/10/25 10:20:40	38
@@ -14,50 +14,12 @@
         [DllImport("user32.dll")]
         private static extern bool LockWorkStation();
         [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 extern bool ExitWindowsEx(win32api.enums.ExitWindows uFlags, win32api.enums.ShutdownReason dwReason);
         #endregion
 
         #region win32api enums
         private class enums
         {
-            //internal enum WMIShutdownRebootFlags
-            //{
-            //    Shutdown = 1,
-            //    Reboot = 2,
-            //}
             [Flags]
             internal enum ExitWindows : uint
             {
@@ -71,7 +33,6 @@
                 Force = 0x04,
                 ForceIfHung = 0x10,
             }
-
             [Flags]
             internal enum ShutdownReason : uint
             {
@@ -117,21 +78,9 @@
         }
         #endregion
 
-
         #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 { 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 Shutdown() { if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) { return win32api.ExitWindowsEx(win32api.enums.ExitWindows.ShutDown, win32api.enums.ShutdownReason.MajorOther & win32api.enums.ShutdownReason.MinorOther); } else { return false; } }
+        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; } }
         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); }

 

  ViewVC Help
Powered by ViewVC 1.1.22