Library code snippets

Shutdown, restart or log off

Using the ExitWindowsEx API call, you can tell windows to shutdown, restart of log off:

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const EWX_FORCE = 4
Private Const EWX_LOGOFF = 0
Private Const EWX_REBOOT = 2
Private Const EWX_SHUTDOWN = 1

Private Sub cmdShutDown()
    ExitWindowsEx EWX_SHUTDOWN, 0
End Sub

You can also make the PC log off, reboot or force a log off (without giving the user a chance to save his/her files), by using the other constants instead of EWX_SHUTDOWN. Note that in Windows 2000 (maybe NT 4 too), EWX_SHUTDOWN and EWX_REBOOT do not work. I presume this is a security measure so that virus writers cannot turn of a critical PC. EWX_LOGOFF and EWX_FORCE still work.

Comments

  1. 25 Nov 2005 at 09:18

    This class module is extremely helpful and works very well - download it from


    http://www.mentalis.org/


    The class is called 'WindowsController'


    Enjoy


    -bcg-

  2. 27 Sep 2003 at 18:21

    'Put ALL of this code in a module


    Private Const TOKENADJUSTPRIVILEGES = &H20
    Private Const TOKENQUERY = &H8
    Private Const SE
    PRIVILEGEENABLED = &H2
    Private Const EWX
    SHUTDOWN As Long = 1
    Private Const EWXFORCE As Long = 4
    Private Const EWX
    REBOOT = 2
    Private Const EWX_POWEROFF = 8


    Private Type LUID
    UsedPart As Long
    IgnoredForNowHigh32BitPart As Long
    End Type


    Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    TheLuid As LUID
    Attributes As Long
    End Type


    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 OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) 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 AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKENPRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKENPRIVILEGES, ReturnLength As Long) As Long



    Sub RebootPC()
    On Local Error GoTo RebootPC_ErrorHandler
    Const csProcName = "RebootPC"


    Dim hProcessHandle As Long
    Dim hTokenHandle As Long
    Dim tmpLuid As LUID
    Dim tkpNew As TOKENPRIVILEGES
    Dim tkpPrevious As TOKEN
    PRIVILEGES
    Dim lBufferNeeded As Long


    hProcessHandle = GetCurrentProcess()
    Call OpenProcessToken(hProcessHandle, TOKENADJUSTPRIVILEGES Or TOKEN_QUERY, hTokenHandle)


    ' Get the LUID for the shutdown privilege
    Call LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid)


    tkpNew.PrivilegeCount = 1 ' One privilege to set
    tkpNew.TheLuid = tmpLuid
    tkpNew.Attributes = SEPRIVILEGEENABLED


    ' Enable the shutdown privilege in the access token of this process.
    lBufferNeeded = 0
    Call AdjustTokenPrivileges(hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded)


    ' Force a Reboot (no option to save files to cancel out)
    Call ExitWindowsEx(EWXPOWEROFF, &HFFFF)      'USE  (EWXFORCE Or EWX_REBOOT, &HFFFF) For these options


    Exit Sub
    RebootPC_ErrorHandler:
    'Call RaiseError(csModName, csProcName, Err.Number, Err.Description)
    Err.Clear
    End Sub



    Just Call the RebootPC sub from a form or send a command via winsock to your client to run this code.



  3. 28 Jul 2003 at 21:05

    I'm writing a security program and I want the administrator to be able to shut off a remote computer in case of a security breach. (Yep, just the opposite reason it's disabled, and contrary to my name. ) How could I do this? And what's EWX_FORCE do?

  4. 14 Jan 2003 at 05:50

    I tryed this code using Visual.net, and sadely it is not working

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Shutdown, restart or log off.

Leave a comment

Sign in or Join us (it's free).