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.
Related articles
Related discussion
-
how do you hide all in VB6
by CapnJack (1 replies)
-
Problem with Input File
by novavb6 (3 replies)
-
How to produce a txt file with a table??
by novavb6 (1 replies)
-
VB6 compatability from XP to Vista
by bronx (1 replies)
-
Fully justify code
by fresh (0 replies)
This class module is extremely helpful and works very well - download it from
http://www.mentalis.org/
The class is called 'WindowsController'
Enjoy
-bcg-
'Put ALL of this code in a module
Private Const TOKENADJUSTPRIVILEGES = &H20
Private Const TOKENQUERY = &H8
Private Const SEPRIVILEGEENABLED = &H2
Private Const EWXSHUTDOWN As Long = 1
Private Const EWXFORCE As Long = 4
Private Const EWXREBOOT = 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 TOKENPRIVILEGES
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.
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?
I tryed this code using Visual.net, and sadely it is not working
This thread is for discussions of Shutdown, restart or log off.