Shutting down windows using win32api
Dieter Deyke
deyke at attbi.com
Sun May 18 14:09:15 EDT 2003
Josh <mlsj at earthlink.net> writes:
> While I have been programming in Python for quite some time, I am a newbie
> at Win 32 API. I spent all of yesterday trying to figure out a way to
> shutdown the local computer using InitiateSystemShutdown. I just couldn't
> do it. Could someone walk me through the steps required(an example script
> would be nice too). I went through the documentation for win32all as well
> as the MSDN website. I understand how I can do it from a C program but how
> do you it do with Python?
>
> Thanks
>
> Josh
I use this batch file to do a file system check with reboot:
@python -x "%~f0" "-*- Python -*-" & goto :EOF
# Initiate an immediate reboot which will check all local drives
import win32api
import win32con
import win32file
value = []
drives = win32file.GetLogicalDrives()
for drive in range(26):
if drives & (1 << drive):
letter = chr(65 + drive)
if win32file.GetDriveType(letter + ":\\") == win32file.DRIVE_FIXED:
value.append("autocheck autochk /p \\??\\" + letter + ":\0")
value.append("autocheck autochk *\0")
value.append("\0")
key = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, "SYSTEM")
key = win32api.RegCreateKey(key, "CurrentControlSet")
key = win32api.RegCreateKey(key, "Control")
key = win32api.RegCreateKey(key, "Session Manager")
win32api.RegSetValueEx(key, "BootExecute", 0, win32con.REG_MULTI_SZ, value)
from ntsecuritycon import *
import win32security
def AdjustPrivilege(priv, enable = 1):
# Get the process token.
flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), flags)
# Get the ID for the system shutdown privilege.
id = win32security.LookupPrivilegeValue(None, priv)
# Now obtain the privilege for this process.
# Create a list of the privileges to be added.
if enable:
newPrivileges = [(id, SE_PRIVILEGE_ENABLED)]
else:
newPrivileges = [(id, 0)]
# and make the adjustment.
win32security.AdjustTokenPrivileges(htoken, 0, newPrivileges)
def Reboot(message = "Rebooting", timeout = 30, bForce = 0, bReboot = 1):
AdjustPrivilege(SE_SHUTDOWN_NAME)
win32api.InitiateSystemShutdown(None, message, timeout, bForce, bReboot)
Reboot(timeout = 0)
--
Dieter Deyke
mailto:deyke at attbi.com mailto:deyke at hotpop.com mailto:deyke at gmx.net
Vs lbh pna ernq guvf, lbh unir jnl gbb zhpu gvzr.
More information about the Python-list
mailing list