Private Declare Function exitwindowsex Lib "user32" (ByVal uflags As Integer, ByVal dwreserved As Integer) As Integer
Const Ewx_force as Short = 4
Const Ewx_logoff as Short = 0
Const Ewx_reboot as Short = 2
Const Ewx_shutdown as Short = 1
Dim RetVal as Integer
' Define ESC key
Const Vk_escape as Short = &h1bs
Private Sub Command1_Click (ByVal Eventsender as System.Object, ByVal EventArgs as System.EventArgs) Handles Command1.click
If option1.checked Then
' Log off the current user
retval = ExitWindowsEx (ewx_force, 0)
ElseIf option2.checked Then
"Shut down the computer
retval = ExitWindowsEx (ewx_shutdown, 0)
ElseIf option3.checked Then
"Reboot
retval = ExitWindowsEx (ewx_reboot, 0)
End If
End Sub
Private Sub Command2_Click (ByVal Eventsender as System.Object, ByVal EventArgs as System.EventArgs) Handles Command2.click
Me.close ()
End Sub
"When the ESC key is pressed, end the application
Private Sub form1_keypress (ByVal Eventsender as System.Object, ByVal EventArgs as System.Windows.Forms.KeyPressEventArgs) Handles mybase.keypress
Dim KeyAscii as Short = ASC (Eventargs.keychar)
If KeyAscii = Vk_escape Then
Me.close ()
End If
If keyascii = 0 Then
eventargs.handled = True
End If
End Sub
This example uses the Exitwindowex () API function to achieve shutdown and restart purposes. In the Exitwindowex () function, the parameter uflags specifies what action to take. The value of the parameter uflags and its description are listed in table 86-2.
Table 86-2 value and description of parameter uflags
Constant name
Value
Description
Ewx_force
4
Terminates all processes, including processes that are not responding, and logs off Windows
Ewx_reboot
2
Reboot the system
Ewx_shutdown
1
Shutting down the system
Ewx_logoff
0
Terminates all running processes and logs off Windows