This is a Premium Blog

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.
Exit Windows - Visual Basic Tutorials

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:
  1. Private Sub Command1_Click()
  2. QuitWindows LOGOFF
  3. End Sub
  4.  
  5. Private Sub Command2_Click()
  6. QuitWindows REBOOT
  7. End Sub
  8.  
  9. Private Sub Command3_Click()
  10. QuitWindows SHUTDOWN
  11. End Sub
  12.  
  13. Private Sub Command4_Click()
  14. QuitWindows POWEROFF
  15. End Sub
  16.  
  17. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  18. If Check1.Value = 0 Then Exit Sub
  19. If UnloadMode = vbAppWindows Then
  20. tmp = MsgBox("Do you want to Shutdown?", vbYesNoCancel, "Confirm Shutdown")
  21. If tmp = vbNo Then Cancel = Not Cancel
  22. End If
  23. End Sub
  24.  
  25. Private Sub Form_Unload(Cancel As Integer)
  26. If Check1.Value = 0 Then Exit Sub
  27. Cancel = 1
  28. 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:
  1. Option Explicit
  2.  
  3. Private Const EWX_FORCE As Long = 4
  4. Private Type LUID
  5. UsedPart As Long
  6. IgnoredForNowHigh32BitPart As Long
  7. End Type
  8.  
  9. Private Type TOKEN_PRIVILEGES
  10. PrivilegeCount As Long
  11. TheLuid As LUID
  12. Attributes As Long
  13. End Type
  14.  
  15. Public Enum ExitWindows
  16. LOGOFF = 0
  17. SHUTDOWN = 1
  18. REBOOT = 2
  19. POWEROFF = 8
  20. End Enum
  21.  
  22. #If False Then
  23. Private LOGOFF, SHUTDOWN, REBOOT, POWEROFF
  24. #End If
  25.  
  26. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
  27. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  28. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  29. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  30. 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
  31.  
  32. Private Sub AdjustToken()
  33. Dim hdlProcessHandle          As Long
  34. Dim hdlTokenHandle            As Long
  35. Dim tmpLuid                   As LUID
  36. Dim tkp                       As TOKEN_PRIVILEGES
  37. Dim tkpNewButIgnored          As TOKEN_PRIVILEGES
  38. Dim lBufferNeeded             As Long
  39.  
  40. hdlProcessHandle = GetCurrentProcess()
  41. OpenProcessToken hdlProcessHandle, (&H20 Or &H8), hdlTokenHandle
  42. LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  43. With tkp
  44. .PrivilegeCount = 1
  45. .TheLuid = tmpLuid
  46. .Attributes = &H2
  47. End With
  48. AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  49. End Sub
  50.  
  51. Public Sub QuitWindows(ByVal aOption As ExitWindows)
  52. AdjustToken
  53. Select Case aOption
  54. Case ExitWindows.LOGOFF
  55. ExitWindowsEx (ExitWindows.LOGOFF Or EWX_FORCE), &HFFFF
  56. Case ExitWindows.REBOOT
  57. ExitWindowsEx (ExitWindows.SHUTDOWN Or EWX_FORCE Or ExitWindows.REBOOT), &HFFFF
  58. Case ExitWindows.SHUTDOWN
  59. ExitWindowsEx (ExitWindows.SHUTDOWN Or EWX_FORCE), &HFFFF
  60. Case ExitWindows.POWEROFF
  61. ExitWindowsEx (ExitWindows.POWEROFF Or EWX_FORCE), &HFFFF
  62. End Select
  63. 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.

If you enjoyed this post Subscribe to our feed

One Comment on “Exit Windows”

  1. Dr.Boom |

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

Leave a Reply