Shut down Windows from a program. 
December 4th, 2006, 02:53 PM
|  | Expert | | Join Date: Jun 2006 Location: Seremban, Malaysia
Posts: 1,630
| | Shut down Windows
For various reasons you may require a shut down of Windows to happen programmatically. For instance if the installation of your program requires system reconfiguration. The following code will do this for you with options to Reboot, etc. -
'Module code - modShutdown
-
-
' Shutdown Flags
-
Const EWX_LOGOFF = 0
-
Const EWX_SHUTDOWN = 1
-
Const EWX_REBOOT = 2
-
Const EWX_FORCE = 4
-
Const SE_PRIVILEGE_ENABLED = &H2
-
Const TokenPrivileges = 3
-
Const TOKEN_ASSIGN_PRIMARY = &H1
-
Const TOKEN_DUPLICATE = &H2
-
Const TOKEN_IMPERSONATE = &H4
-
Const TOKEN_QUERY = &H8
-
Const TOKEN_QUERY_SOURCE = &H10
-
Const TOKEN_ADJUST_PRIVILEGES = &H20
-
Const TOKEN_ADJUST_GROUPS = &H40
-
Const TOKEN_ADJUST_DEFAULT = &H80
-
Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
-
Const ANYSIZE_ARRAY = 1
-
Private Type LARGE_INTEGER
-
lowpart As Long
-
highpart As Long
-
End Type
-
Private Type Luid
-
lowpart As Long
-
highpart As Long
-
End Type
-
Private Type LUID_AND_ATTRIBUTES
-
'pLuid As Luid
-
pLuid As LARGE_INTEGER
-
Attributes As Long
-
End Type
-
Private Type TOKEN_PRIVILEGES
-
PrivilegeCount As Long
-
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
-
End Type
-
Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
-
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
-
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
-
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LARGE_INTEGER) As Long
-
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
-
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
-
Private Declare Function GetLastError Lib "kernel32" () As Long
-
-
Public Function InitiateShutdown(ByVal Machine As String, _
-
Optional Force As Variant, _
-
Optional Restart As Variant, _
-
Optional AllowLocalShutdown As Variant, _
-
Optional Delay As Variant, _
-
Optional Message As Variant) As Boolean
-
-
Dim hProc As Long
-
Dim OldTokenStuff As TOKEN_PRIVILEGES
-
Dim OldTokenStuffLen As Long
-
Dim NewTokenStuff As TOKEN_PRIVILEGES
-
Dim NewTokenStuffLen As Long
-
Dim pSize As Long
-
If IsMissing(Force) Then Force = False
-
If IsMissing(Restart) Then Restart = True
-
If IsMissing(AllowLocalShutdown) Then AllowLocalShutdown = False
-
If IsMissing(Delay) Then Delay = 0
-
If IsMissing(Message) Then Message = ""
-
'Make sure the Machine-name doesn't start with '\\'
-
If InStr(Machine, "\\") = 1 Then
-
Machine = Right(Machine, Len(Machine) - 2)
-
End If
-
'check if it's the local machine that's going to be shutdown
-
If (LCase(GetMachineName) = LCase(Machine)) Then
-
'may we shut this computer down?
-
If AllowLocalShutdown = False Then Exit Function
-
'open access token
-
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hProc) = 0 Then
-
MsgBox "OpenProcessToken Error: " & GetLastError()
-
Exit Function
-
End If
-
'retrieve the locally unique identifier to represent the Shutdown-privilege name
-
If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, OldTokenStuff.Privileges(0).pLuid) = 0 Then
-
MsgBox "LookupPrivilegeValue Error: " & GetLastError()
-
Exit Function
-
End If
-
NewTokenStuff = OldTokenStuff
-
NewTokenStuff.PrivilegeCount = 1
-
NewTokenStuff.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
-
NewTokenStuffLen = Len(NewTokenStuff)
-
pSize = Len(NewTokenStuff)
-
'Enable shutdown-privilege
-
If AdjustTokenPrivileges(hProc, False, NewTokenStuff, NewTokenStuffLen, OldTokenStuff, OldTokenStuffLen) = 0 Then
-
MsgBox "AdjustTokenPrivileges Error: " & GetLastError()
-
Exit Function
-
End If
-
'initiate the system shutdown
-
If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
-
Exit Function
-
End If
-
NewTokenStuff.Privileges(0).Attributes = 0
-
'Disable shutdown-privilege
-
If AdjustTokenPrivileges(hProc, False, NewTokenStuff, Len(NewTokenStuff), OldTokenStuff, Len(OldTokenStuff)) = 0 Then
-
Exit Function
-
End If
-
Else
-
'initiate the system shutdown
-
If InitiateSystemShutdown("\\" & Machine, Message, Delay, Force, Restart) = 0 Then
-
Exit Function
-
End If
-
End If
-
InitiateShutdown = True
-
End Function
-
-
Function GetMachineName() As String
-
Dim sLen As Long
-
'create a buffer
-
GetMachineName = Space(100)
-
sLen = 100
-
'retrieve the computer name
-
If GetComputerName(GetMachineName, sLen) Then
-
GetMachineName = Left(GetMachineName, sLen)
-
End If
-
End Function
-
-
-
'Form code - frmShutdown
-
-
Private Sub cmdShutdownNow_Click()
-
modShutdown.InitiateShutdown GetMachineName, True, False, True, 60, "Message to state reason for shutdown!"
-
End Sub
-
Last edited by RedSon; November 21st, 2007 at 07:08 PM.
| 
December 31st, 2007, 12:00 PM
|  | Needs Regular Fix | | Join Date: Aug 2007
Posts: 493
| | | re: Shut down Windows from a program.
Does this work on all windows Os or xp only ....?
It does not state
| 
January 24th, 2008, 11:51 AM
| | Newbie | | Join Date: Jan 2008 Location: Phillipines
Posts: 5
| | | re: Shut down Windows from a program.
is this reliable to windows vista?
| 
February 29th, 2008, 06:37 AM
| | Newbie | | Join Date: Feb 2008
Posts: 1
| | | re: Shut down Windows from a program.
avoid more codes and make easier with less codes
| 
April 13th, 2008, 06:50 PM
| | Newbie | | Join Date: Jan 2006
Posts: 1
| | | re: Shut down Windows from a program.
i want to write a program in VB6.0 for many users so that each usher has a separate password from another user. I want the users to login only three times after which their password will become invalid. I need your help on how to write the code. One textbox will be used for all of them.
I need your help on how to write a VB code that can save the content of a form so that it can be retrieved after shutting down the system or after exitting the VB environment. Please, your help is highly and urgently needed. Thanks
| 
April 18th, 2008, 11:35 AM
| | Newbie | | Join Date: Apr 2008
Posts: 1
| | | re: Shut down Windows from a program.
plz add the shutdown coding to lern others
| 
September 23rd, 2008, 02:21 AM
| | Member | | Join Date: Nov 2007 Location: Alberta, Canada
Posts: 59
| | | re: Shut down Windows from a program.
of course you can also just run that one executive in the windows folder that shuts down windows...but i believe it only works on xp and below...or maybe its 2000 and below
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|