Reboot the computer
By: Unknown Date: 24/09/1998

This program will cause the computer to be rebooted. In order to test this example, create a new VB project consisting of only one form. Place a command button on the form and set it's caption to Reboot Computer. Then place the following API declaration into the form's declarations section:

Declare Function ExitWindows Lib "User32" (ByVal dwReturnCode _
As Long, ByVal wReserved As Integer) As Integer

The next step, is to create a form-level function that will actually cause the computer to be restarted.

Function RebootSystem () As Integer
    Dim i As Integer
    Dim EW_REBOOTSYSTEM As Long
    EW_REBOOTSYSTEM = &H43
    i = ExitWindows(EW_REBOOTSYSTEM, 0)
End Function

Finally, place some code behind the command button's click event. This code will prompt the user for confirmation before executing. The code for the command button looks like the following:

Sub Command1_Click ()
    Dim x As Integer
    If MsgBox("This routine will Reboot the Computer!" _
    & Chr$(13) & Chr$(10) & "Are you sure you want to do this now?", _
    36, "Restart Windows?") = 6 Then
        x = RebootSystem()
        If Not x Then MsgBox "Some program(s) refused to terminate", _
        48, "VB4UandME Demo"
    End If
End Sub

Be sure to save your project just in case it works perfectly the first time! Run the program, click the button, and tell it yes. Make sure you have saved everything you need saved first!

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
Webmaster's e-mail: webmaster@vbprogzone.cjb.net
Hosted by InWind: www.inwind.it