Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. switch off computer

switch off computer

Scheduled Pinned Locked Moved C#
tutorialquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    likefood
    wrote on last edited by
    #1

    I did a search to see if this has already been answered, but didn't find anything (if it's already been answered, please just point me in the right direction). I need to find out how to switch off the computer at runtime, meaning from a program, not by pushing the button on the tower :) . I suppose I could just create a process using shutdown.exe with args, but that actually shuts down Windows, and I want to bypass that. I have seen it done in the past (but I don't remember what program did it). I'm hoping there's some unmanaged function call (to some dll somewhere) that will simply power off the machine with no dialogs or anything. Just beeooooop, the machine is off. Anyone?

    -Daniel Typing too fast fro my owngood

    L G L X 4 Replies Last reply
    0
    • L likefood

      I did a search to see if this has already been answered, but didn't find anything (if it's already been answered, please just point me in the right direction). I need to find out how to switch off the computer at runtime, meaning from a program, not by pushing the button on the tower :) . I suppose I could just create a process using shutdown.exe with args, but that actually shuts down Windows, and I want to bypass that. I have seen it done in the past (but I don't remember what program did it). I'm hoping there's some unmanaged function call (to some dll somewhere) that will simply power off the machine with no dialogs or anything. Just beeooooop, the machine is off. Anyone?

      -Daniel Typing too fast fro my owngood

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't see why shutdown.exe would not suit this requirement.. to be honest I don't know of another way, and if it exists I'm not sure it would be entirely safe to the machine. For no dialog, couldn't shutdown.exe -s -f work as you described?

      L L 2 Replies Last reply
      0
      • L Lost User

        I don't see why shutdown.exe would not suit this requirement.. to be honest I don't know of another way, and if it exists I'm not sure it would be entirely safe to the machine. For no dialog, couldn't shutdown.exe -s -f work as you described?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        EliottA wrote:

        to be honest I don't know of another way, and if it exists I'm not sure it would be entirely safe to the machine.

        ExitWindowsEx[^] API does the job. regards

        L 1 Reply Last reply
        0
        • L likefood

          I did a search to see if this has already been answered, but didn't find anything (if it's already been answered, please just point me in the right direction). I need to find out how to switch off the computer at runtime, meaning from a program, not by pushing the button on the tower :) . I suppose I could just create a process using shutdown.exe with args, but that actually shuts down Windows, and I want to bypass that. I have seen it done in the past (but I don't remember what program did it). I'm hoping there's some unmanaged function call (to some dll somewhere) that will simply power off the machine with no dialogs or anything. Just beeooooop, the machine is off. Anyone?

          -Daniel Typing too fast fro my owngood

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          Here is an example: Rename Start Button[^]

          Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

          L 1 Reply Last reply
          0
          • L Lost User

            I don't see why shutdown.exe would not suit this requirement.. to be honest I don't know of another way, and if it exists I'm not sure it would be entirely safe to the machine. For no dialog, couldn't shutdown.exe -s -f work as you described?

            L Offline
            L Offline
            likefood
            wrote on last edited by
            #5

            shutdown.exe would shut down Windows and then turn off the machine (depending on the arguments). However, I wish to bypass the "shut down Windows" part and just turn off the machine from the program. Yes, I know, it's not healthy. But I have a need regardless.

            -Daniel Typing too fast fro my owngood

            1 Reply Last reply
            0
            • L Lost User

              EliottA wrote:

              to be honest I don't know of another way, and if it exists I'm not sure it would be entirely safe to the machine.

              ExitWindowsEx[^] API does the job. regards

              L Offline
              L Offline
              likefood
              wrote on last edited by
              #6

              I'll look into this one, thank you!

              -Daniel Typing too fast fro my owngood

              1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                Here is an example: Rename Start Button[^]

                Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

                L Offline
                L Offline
                likefood
                wrote on last edited by
                #7

                I'll look into this one, too, thank you!

                -Daniel Typing too fast fro my owngood

                1 Reply Last reply
                0
                • L likefood

                  I did a search to see if this has already been answered, but didn't find anything (if it's already been answered, please just point me in the right direction). I need to find out how to switch off the computer at runtime, meaning from a program, not by pushing the button on the tower :) . I suppose I could just create a process using shutdown.exe with args, but that actually shuts down Windows, and I want to bypass that. I have seen it done in the past (but I don't remember what program did it). I'm hoping there's some unmanaged function call (to some dll somewhere) that will simply power off the machine with no dialogs or anything. Just beeooooop, the machine is off. Anyone?

                  -Daniel Typing too fast fro my owngood

                  L Offline
                  L Offline
                  likefood
                  wrote on last edited by
                  #8

                  Looking up WMI, it appears that the following should work like I want:

                  ManagementClass mc = new ManagementClass("CIM_PowerSupply");
                  object[] methodArgs = { 6, null };//with 6 being to power off
                  object result = mc.InvokeMethod("SetPowerState", methodArgs);

                  However, the program crashes with a ManagementException, saying "This method is not implemented in any class." And, Microsoft's article on the SetPowerState function says "This method is currently not implemented by WMI. To use this method, you must implement it in your own provider." It's kind of cruel to tease me with a function that doesn't actually do anything... Anyone know how it's supposed to be "implemented"?

                  -Daniel Typing too fast fro my owngood

                  L 1 Reply Last reply
                  0
                  • L likefood

                    Looking up WMI, it appears that the following should work like I want:

                    ManagementClass mc = new ManagementClass("CIM_PowerSupply");
                    object[] methodArgs = { 6, null };//with 6 being to power off
                    object result = mc.InvokeMethod("SetPowerState", methodArgs);

                    However, the program crashes with a ManagementException, saying "This method is not implemented in any class." And, Microsoft's article on the SetPowerState function says "This method is currently not implemented by WMI. To use this method, you must implement it in your own provider." It's kind of cruel to tease me with a function that doesn't actually do anything... Anyone know how it's supposed to be "implemented"?

                    -Daniel Typing too fast fro my owngood

                    L Offline
                    L Offline
                    likefood
                    wrote on last edited by
                    #9

                    I guess I have some reading to do... http://msdn.microsoft.com/en-us/library/cc268228.aspx Thank you for pointing me toward System.Managment, all!

                    -Daniel Typing too fast fro my owngood

                    1 Reply Last reply
                    0
                    • L likefood

                      I did a search to see if this has already been answered, but didn't find anything (if it's already been answered, please just point me in the right direction). I need to find out how to switch off the computer at runtime, meaning from a program, not by pushing the button on the tower :) . I suppose I could just create a process using shutdown.exe with args, but that actually shuts down Windows, and I want to bypass that. I have seen it done in the past (but I don't remember what program did it). I'm hoping there's some unmanaged function call (to some dll somewhere) that will simply power off the machine with no dialogs or anything. Just beeooooop, the machine is off. Anyone?

                      -Daniel Typing too fast fro my owngood

                      X Offline
                      X Offline
                      Xmen Real
                      wrote on last edited by
                      #10

                      yes shutdown sucks with shutdown.exe, specially in older windows version eg. XP here is the code i use to shut my pc down ;)

                      //using System.Management;
                      void Shutdown()
                      {
                      ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
                      ManagementBaseObject inParams, outParams;
                      int result;
                      W32_OS.Scope.Options.EnablePrivileges = true;
                      foreach (ManagementObject obj in W32_OS.GetInstances())
                      {
                      inParams = obj.GetMethodParameters("Win32Shutdown");
                      inParams["Flags"] = ShutDown.ForcedShutdown;
                      inParams["Reserved"] = 0;
                      outParams = obj.InvokeMethod("Win32Shutdown", inParams, null);
                      result = Convert.ToInt32(outParams["returnValue"]);
                      if (result != 0) throw new Win32Exception(result);
                      }
                      }
                      public enum ShutDown
                      {
                      LogOff = 0,
                      Shutdown = 1,
                      Reboot = 2,
                      ForcedLogOff = 4,
                      ForcedShutdown = 5,
                      ForcedReboot = 6,
                      PowerOff = 8,
                      ForcedPowerOff = 12
                      }

                      When you test this, save all your work. There is no way to stop this thing.

                      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

                      L 1 Reply Last reply
                      0
                      • X Xmen Real

                        yes shutdown sucks with shutdown.exe, specially in older windows version eg. XP here is the code i use to shut my pc down ;)

                        //using System.Management;
                        void Shutdown()
                        {
                        ManagementClass W32_OS = new ManagementClass("Win32_OperatingSystem");
                        ManagementBaseObject inParams, outParams;
                        int result;
                        W32_OS.Scope.Options.EnablePrivileges = true;
                        foreach (ManagementObject obj in W32_OS.GetInstances())
                        {
                        inParams = obj.GetMethodParameters("Win32Shutdown");
                        inParams["Flags"] = ShutDown.ForcedShutdown;
                        inParams["Reserved"] = 0;
                        outParams = obj.InvokeMethod("Win32Shutdown", inParams, null);
                        result = Convert.ToInt32(outParams["returnValue"]);
                        if (result != 0) throw new Win32Exception(result);
                        }
                        }
                        public enum ShutDown
                        {
                        LogOff = 0,
                        Shutdown = 1,
                        Reboot = 2,
                        ForcedLogOff = 4,
                        ForcedShutdown = 5,
                        ForcedReboot = 6,
                        PowerOff = 8,
                        ForcedPowerOff = 12
                        }

                        When you test this, save all your work. There is no way to stop this thing.

                        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

                        L Offline
                        L Offline
                        likefood
                        wrote on last edited by
                        #11

                        Thank you, Xmen, but the PowerOff and ForcedPowerOff enum values are deceptive. Yes, they do power off the computer, but they also shut down Windows before then. I was looking for a way to power off the computer without wasting time shutting down Windows (the computer just goes silent right after I double-click something.exe on my desktop). But, thanks anyway!

                        -Daniel Typing too fast fro my owngood

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups