Vb. NET implementation shutdown and reboot

Source: Internet
Author: User
Tags integer terminates
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

' Turn off 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





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.