[python-win32] Standby/Hibernate

Rickey, Kyle W Kyle.Rickey at bakerhughes.com
Thu Jun 5 21:00:05 CEST 2008


Thanks Tim. That did the trick. I need to do more digging on MSDN. Most
of the google results I hit were people trying to prevent
standby/hibernate. Thanks again.

-Kyle Rickey

-----Original Message-----
From: python-win32-bounces at python.org
[mailto:python-win32-bounces at python.org] On Behalf Of Tim Golden
Sent: Thursday, June 05, 2008 3:30 AM
Cc: python-win32 at python.org
Subject: Re: [python-win32] Standby/Hibernate

Rickey, Kyle W wrote:
> What do I need to do to put my local computer into standby? I found
the
> function:
> 
> win32api.InitiateSystemShutdown("INT8Y4Y3B1", "Tom Sucks", 300, False,
> False)
> 
> but it seems to only shutdown/restart.

You need the SetSystemPowerState [1] function from kernel32. 
(And your process will need to enable SeShutdown as well).

<code>
import ctypes
import win32api
import win32security

#
# Enable the SeShutdown privilege (which must be present in your
# token in the first place)
#
priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES |
win32security.TOKEN_QUERY
hToken = win32security.OpenProcessToken (
 win32api.GetCurrentProcess (),
 priv_flags
)
priv_id = win32security.LookupPrivilegeValue (
   None, 
   win32security.SE_SHUTDOWN_NAME
)
old_privs = win32security.AdjustTokenPrivileges (
 hToken,
 0,
 [(privilege_id, win32security.SE_PRIVILEGE_ENABLED)]
)

#
# Params:
# True=> Standby; False=> Hibernate
# True=> Force closedown; False=> Don't force
#
ctypes.windll.kernel32.SetSystemPowerState (True, True)

</code>


TJG

[1] http://msdn.microsoft.com/en-us/library/aa373206(VS.85).aspx

_______________________________________________
python-win32 mailing list
python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32


More information about the python-win32 mailing list