Exit Windows
Written on June 15, 2007 by admin
We will create program that can logoff, reboot, shutdown, and power off computer. The graphical is really simple.

When we click Shutdown, the program will shutdown your computer without any warning!!
Checkbox is for blocking exit windows from startmenu.
Code for this program divided by 2 parts, first is form1 and second is module file.
Code for form1 is:
Visual Basic:
-
Private Sub Command1_Click()
-
QuitWindows LOGOFF
-
End Sub
-
-
Private Sub Command2_Click()
-
QuitWindows REBOOT
-
End Sub
-
-
Private Sub Command3_Click()
-
QuitWindows SHUTDOWN
-
End Sub
-
-
Private Sub Command4_Click()
-
QuitWindows POWEROFF
-
End Sub
-
-
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
-
If Check1.Value = 0 Then Exit Sub
-
If UnloadMode = vbAppWindows Then
-
tmp = MsgBox("Do you want to Shutdown?", vbYesNoCancel, "Confirm Shutdown")
-
If tmp = vbNo Then Cancel = Not Cancel
-
End If
-
End Sub
-
-
Private Sub Form_Unload(Cancel As Integer)
-
If Check1.Value = 0 Then Exit Sub
-
Cancel = 1
-
End Sub
Procedure for Form_Unload and Form_QueryUnload is for canceling closing window and exit from Windows from start menu.
Code for module is:
Visual Basic:
-
Option Explicit
-
-
Private Const EWX_FORCE As Long = 4
-
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
-
-
Public Enum ExitWindows
-
LOGOFF = 0
-
SHUTDOWN = 1
-
REBOOT = 2
-
POWEROFF = 8
-
End Enum
-
-
#If False Then
-
Private LOGOFF, SHUTDOWN, REBOOT, POWEROFF
-
#End If
-
-
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags 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 TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
-
-
Private Sub AdjustToken()
-
Dim hdlProcessHandle As Long
-
Dim hdlTokenHandle As Long
-
Dim tmpLuid As LUID
-
Dim tkp As TOKEN_PRIVILEGES
-
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
-
Dim lBufferNeeded As Long
-
-
hdlProcessHandle = GetCurrentProcess()
-
OpenProcessToken hdlProcessHandle, (&H20 Or &H8), hdlTokenHandle
-
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
-
With tkp
-
.PrivilegeCount = 1
-
.TheLuid = tmpLuid
-
.Attributes = &H2
-
End With
-
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
-
End Sub
-
-
Public Sub QuitWindows(ByVal aOption As ExitWindows)
-
AdjustToken
-
Select Case aOption
-
Case ExitWindows.LOGOFF
-
ExitWindowsEx (ExitWindows.LOGOFF Or EWX_FORCE), &HFFFF
-
Case ExitWindows.REBOOT
-
ExitWindowsEx (ExitWindows.SHUTDOWN Or EWX_FORCE Or ExitWindows.REBOOT), &HFFFF
-
Case ExitWindows.SHUTDOWN
-
ExitWindowsEx (ExitWindows.SHUTDOWN Or EWX_FORCE), &HFFFF
-
Case ExitWindows.POWEROFF
-
ExitWindowsEx (ExitWindows.POWEROFF Or EWX_FORCE), &HFFFF
-
End Select
-
End Sub
AdjustToken procedure in Windows NT is to get privileges to shutdown. If no, QuitWindows procedure will not do anything. In Windows 98 without AdjustToken, QuitWindows procedure will start.



???…. ???? ?????-?-?-??! ??????? ????????)))