/[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:25:32	35
+++ trunk/NexusPowerCommand/win32api.cs	2011/10/25 09:59:44	36
@@ -18,23 +18,33 @@
 
         private static void WMIShutdownReboot(win32api.enums.WMIShutdownRebootFlags flags)
         {
-            ManagementBaseObject mboShutdown = null;
-            ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
-            mcWin32.Get();
-
-            // You can't shutdown without security privileges
-            mcWin32.Scope.Options.EnablePrivileges = true;
-            mcWin32.Scope.Options.Impersonation = ImpersonationLevel.Impersonate;
-            ManagementBaseObject mboShutdownParams =
-                     mcWin32.GetMethodParameters("Win32Shutdown");
-
-            // Flag 1 means we want to shut down the system. Use "2" to reboot.
-            mboShutdownParams["Flags"] = (int)flags;
-            mboShutdownParams["Reserved"] = "0";
-            foreach (ManagementObject manObj in mcWin32.GetInstances())
+            if (TokenAdjuster.SetPrivilege("SeShutdownPrivilege", true)) // Privilege tobe enabled (here SeShutdownPrivilege)
             {
-                mboShutdown = manObj.InvokeMethod("Win32Shutdown",
-                                               mboShutdownParams, null);
+                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;
+                }
             }
         }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22