I am looking for some code that will let me shutdown/restart/log off/standby my windows system.
Printable View
I am looking for some code that will let me shutdown/restart/log off/standby my windows system.
You can find lots of code snippets in our CodeBank... :wave:
http://www.vbforums.com/search.php?searchid=2445048
I didn't write this but I've been using it for years:vb Code:
Option Explicit Private Type TOKEN_PRIVILEGES PrivilegeCount As Long LuidUDT As LUID Attributes As Long End Type Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Declare Function GetVersion Lib "kernel32" () As Long Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long ' Shut down windows, and optional reboot it ' if the 2nd argument is True, no WM_QUERYENDSESSION and WM_ENDSESSION ' messages are sent to active applications Public Sub ShutDownWindows(Optional ByVal Reboot As Boolean = False, Optional ByVal Force As Boolean) Const EWX_LOGOFF = 0 Const EWX_SHUTDOWN = 1 Const EWX_REBOOT = 2 Const EWX_FORCE = 4 Const EWX_POWEROFF = 8 Const TOKEN_ADJUST_PRIVILEGES = &H20 Const TOKEN_QUERY = &H8 Const SE_PRIVILEGE_ENABLED = &H2 Dim hToken As Long Dim tp As TOKEN_PRIVILEGES Dim flags As Long ' Windows NT/2000 require a special treatment ' to ensure that the calling process has the ' privileges to shut down the system ' under NT the high-order bit (that is, the sign bit) ' of the value retured by GetVersion is cleared If GetVersion() >= 0 Then ' Open this process for adjusting its privileges OpenProcessToken GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hToken ' Get the LUID for shutdown privilege. ' retrieves the locally unique identifier (LUID) used ' to locally represent the specified privilege name ' (first argument = "" means the local system) LookupPrivilegeValue "", "SeShutdownPrivilege", tp.LuidUDT ' complete the TOKEN_PRIVILEGES structure with the # of ' privileges and the desired attribute tp.PrivilegeCount = 1 tp.Attributes = SE_PRIVILEGE_ENABLED ' enables or disables privileges in the specified access token ' last 3 arguments are zero because we aren't interested ' in previous privilege attributes. AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0& End If ' prepare shutdown flags flags = EWX_SHUTDOWN If Reboot Then flags = flags Or EWX_REBOOT Else flags = flags Or EWX_POWEROFF End If If Force Then flags = flags Or EWX_FORCE ' finally, you can shut down Windows ExitWindowsEx flags, &HFFFF End Sub
What is the difference between PowerOff and Shutdown?Code:#
Const EWX_LOGOFF = 0
#
Const EWX_SHUTDOWN = 1
#
Const EWX_REBOOT = 2
#
Const EWX_FORCE = 4
#
Const EWX_POWEROFF = 8
#
Also, how can i get the system to standby?
I never played around with the constants, though I'm glad I preserved them in the code for just such an occasion.
I'm guessing that PowerOff puts the computer in that sleep mode where the drive spins down, which is I believe what you mean by standby. I don't know the actual official term for it; maybe hibernate?
EDIT: Of course that can't be right because PowerOff actually shuts the computer down. Maybe Shutdown does the hibernate thing?
Okay, a little poking around explains it.
Since this could conceivably be before your (or a lurker's) time, I'll explain the difference.
Back in the day when Windows 95 first came out, most computers couldn't completely power down except by having the user physically press the power button on the case. Before Windows 95 the user had to take it upon himself to ensure no programs were running when he clicked the power button. This meant you'd have to manually exit Windows 3.1 until you saw a DOS prompt, and only then could you power off.
Windows 95 introduced the "Shutdown" concept that had already existed on Macs for years. The only problem was that computers themselves didn't have the required circuitry to physically turn themselves off. So in Windows 95 what typically happened is you'd click Shutdown from the Start Menu and Windows would do its shutdown thing then leave the computer sitting at a screen that said "It is now safe to turn off your computer." The user would then flip the switch on the case to actually turn it off.
Very quickly after that manufacturers added the necessary circuitry to let computers turn themselves off, so instead of telling the user to turn it off it'd just turn itself off. So that's the difference.
Shutdown: Shut down Windows and display the "It is now safe to turn off your computer" screen.
PowerOff: Shut down Windows and turn off the computer.
thanks for your help.