Thread: Put the computer to sleep

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Put the computer to sleep

    Hi! Is there any way to put the computer to sleep (or hibernate or power off also for that matter) from within a program, with the same result as pressing the sleep (or hibernate or shut down) button in the start menu?

    Sometimes I want the computer to finish a certain task or wait for a certain amount of time, while I am not by the computer, and then go into sleep mode. The task being executed before putting the computer to sleep is not important here.

    I have searched quite a lot about this but there doesn't seem to be very much information about it.
    Come on, you can do it! b( ~_')

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Well, that information is available in the Platform SDK (Windows API).
    You can do a search on MSDN.
    MSDN | Microsoft Development, Subscriptions, Resources, and More

    from SDK:
    Code:
    SetSuspendState:
    
    The SetSuspendState function suspends the system by shutting power down. 
    Depending on the Hibernate parameter, 
    the system either enters a suspend (sleep) state or hibernation (S4).
    
    BOOLEAN SetSuspendState( BOOLEAN Hibernate,
                                BOOLEAN ForceCritical,
                                BOOLEAN DisableWakeEvent);
    Code:
    SetSystemPowerState:
    
    The SetSystemPowerState function suspends the system by shutting power down. 
    Depending on the ForceFlag parameter, 
    the function either suspends operation immediately
    or requests permission from all applications and device drivers before doing so.
    
    BOOL SetSystemPowerState( BOOL fSuspend,
                                 BOOL fForce);
    Also, See: Power Management Functions

    There may even be other API functions as well, I haven't looked.
    Last edited by Steve A.; 03-20-2011 at 07:17 PM. Reason: clarity

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    It's much easier to run a batch file, more universal too (eg works with programs you don't have source for)

    Code:
    @echo off
    %*
    shutdown /h
    Call it hibernateafter.cmd or whatever and use it like this:
    Code:
    C:\Users\Adrian>hibernateafter mycmdlineprog that takes "many cmd parameters"
    or
    C:\Users\Adrian>hibernateafter winver
    When the program closes, shutdown /h hibernates your computer.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by adeyblue
    It's much easier to just run a batch file
    Sure, you could just shell to the system...,
    system("hibernate.cmd")
    or
    ShellExecute(..., hibernate.cmd, ...)

    both cheap'n easy, or, do it programmatically using the API.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Calling shell commands from within a program opens security issues if the machine is compromised. It's never advisable if there's a clear solution that can be done from within the code. Post #2 is the way to do it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TriKri View Post
    Hi! Is there any way to put the computer to sleep (or hibernate or power off also for that matter) from within a program, with the same result as pressing the sleep (or hibernate or shut down) button in the start menu?

    Sometimes I want the computer to finish a certain task or wait for a certain amount of time, while I am not by the computer, and then go into sleep mode. The task being executed before putting the computer to sleep is not important here.

    I have searched quite a lot about this but there doesn't seem to be very much information about it.
    SetSystemPowerState

    (30 seconds on google)

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by CommonTater
    (30 seconds on google)
    Hmm..., must have a slow connection.
    LOL !

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs up

    Thanks! That seems like it will do it. Well, I guess it is easy to find it if you know what to look for. I actually made searches at both google and msdn, but I didn't manage to find anything I thought was that useful; maybe I'm just bad at searching. But once again, thanks for the help; seems like I have plenty of alternatives now!
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An efficient approach to TicTacToe AI?
    By xofvc4rqb in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2011, 05:09 PM
  2. computer sccience
    By valthyx in forum General Discussions
    Replies: 13
    Last Post: 07-29-2009, 08:22 AM
  3. some odd computer problems
    By DavidP in forum Tech Board
    Replies: 14
    Last Post: 03-31-2008, 06:25 AM
  4. Computer Problems
    By fac7 in forum Tech Board
    Replies: 13
    Last Post: 03-23-2004, 01:22 AM

Tags for this Thread