{"id":250,"date":"2006-03-20T14:56:32","date_gmt":"2006-03-20T13:56:32","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=250"},"modified":"2006-03-22T08:19:46","modified_gmt":"2006-03-22T07:19:46","slug":"shutting-down-a-pc-using-wmi-and-c","status":"publish","type":"post","link":"http:\/\/www.craigmurphy.com\/blog\/?p=250","title":{"rendered":"Shutting down a PC using WMI and C#"},"content":{"rendered":"<p>I found myself needing to re-work some code that was originally part of my <a href=\"http:\/\/www.craigmurphy.com\/bug\">WMI presentation<\/a> from 2003.<\/p>\n<p>After a little bit of re-writing and a little bit of searching, I re-wrote my original Delphi routine, which looked like this:<\/p>\n<p>[code lang=&#8221;Pascal&#8221;]<br \/>\n[Delphi]<br \/>\nprocedure TfrmMain.btnShutdownClick(Sender: TObject);<br \/>\nvar<br \/>\n  wmiLocator:   TSWbemLocator;<br \/>\n  wmiServices:  ISWbemServices;<br \/>\n  wmiObjectSet: ISWbemObjectSet;<br \/>\n  wmiObject:    ISWbemObject;<br \/>\n  Enum:         IEnumVariant;<br \/>\n  ovVar:        OleVariant;<br \/>\n  lwValue:      LongWord;<\/p>\n<p>begin<br \/>\n  wmiLocator  := TSWbemLocator.Create(self);<br \/>\n  wmiServices := wmiLocator.ConnectServer( edtComputer.Text,  &#8216;root\\cimv2&#8217;,  edtUser.Text,  edtPass.Text,  &#8221;, &#8221;, 0, nil);<\/p>\n<p>  wmiServices.Security_.Privileges.Add(wbemPrivilegeShutdown, True);<br \/>\n  wmiObjectSet := wmiServices.ExecQuery(&#8216;SELECT * FROM Win32_OperatingSystem WHERE Primary=True&#8217;, &#8216;WQL&#8217;, wbemFlagReturnImmediately, nil);<br \/>\n  Enum :=  (wmiObjectSet._NewEnum) as IEnumVariant;<br \/>\n  while (Enum.Next(1, ovVar, lwValue) = S_OK) do  begin<br \/>\n    wmiObject := IUnknown(ovVar) as SWBemObject;<br \/>\n    wmiObject.ExecMethod_(&#8216;Shutdown&#8217;, nil, 0, nil);<br \/>\n  end;<\/p>\n<p>  wmiLocator.Free;<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p>&#8230;it now looks like this:<\/p>\n<p>[code lang=&#8221;C&#8217;#&#8221;]<br \/>\n[C#]<br \/>\n  using System.Management;      <\/p>\n<p>  public enum ShutDown<br \/>\n        {<br \/>\n            LogOff = 0,<br \/>\n            Shutdown = 1,<br \/>\n            Reboot = 2,<br \/>\n            ForcedLogOff = 4,<br \/>\n            ForcedShutdown = 5,<br \/>\n            ForcedReboot = 6,<br \/>\n            PowerOff = 8,<br \/>\n            ForcedPowerOff = 12<br \/>\n        }<\/p>\n<p>&#8230;<\/p>\n<p>            ManagementClass W32_OS = new ManagementClass(&#8220;Win32_OperatingSystem&#8221;);<br \/>\n            ManagementBaseObject inParams, outParams;<br \/>\n            int result;<\/p>\n<p>            W32_OS.Scope.Options.EnablePrivileges = true;<br \/>\n            foreach(ManagementObject obj in W32_OS.GetInstances())<br \/>\n            {<br \/>\n                inParams = obj.GetMethodParameters(&#8220;Win32Shutdown&#8221;);<br \/>\n                inParams[&#8220;Flags&#8221;] = ForcedShutdown;<br \/>\n                inParams[&#8220;Reserved&#8221;] = 0;<\/p>\n<p>                outParams = obj.InvokeMethod(&#8220;Win32Shutdown&#8221;, inParams, null);<br \/>\n                result = Convert.ToInt32(outParams[&#8220;returnValue&#8221;]);<br \/>\n                if (result !=0) throw new Win32Exception(result);<br \/>\n            }<br \/>\n[\/code]<\/p>\n<p>Here&#8217;s a <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/wmisdk\/wmi\/win32shutdown_method_in_class_win32_operatingsystem.asp\">link<\/a> to more information about this on the MSDN.<\/p>\n<p><del datetime=\"2006-03-22T07:19:34+00:00\"><em>[job for tonight &#8211; sort out this code format&#8230;once and for all]<\/em><\/del><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I found myself needing to re-work some code that was originally part of my WMI presentation from 2003. After a little bit of re-writing and a little bit of searching, I re-wrote my original Delphi routine, which looked like this: [code lang=&#8221;Pascal&#8221;] [Delphi] procedure TfrmMain.btnShutdownClick(Sender: TObject); var wmiLocator: TSWbemLocator; wmiServices: ISWbemServices; wmiObjectSet: ISWbemObjectSet; wmiObject: ISWbemObject; &hellip; <a href=\"http:\/\/www.craigmurphy.com\/blog\/?p=250\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Shutting down a PC using WMI and C#<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":0,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}