Restart Windows
By: Unknown Date: 24/09/1998

This program will cause Windows to be restarted. 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 Restart Windows. Then place the following API declaration into the form's declarations section:

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

The next step, is to create a form-level function that will actually cause Windows to be restarted. I have coded the function as follows:

Function RestartWindows () As Integer
    Dim i As Integer
    i = ExitWindows(EW_RESTARTWINDOWS, 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 Restart Windows!" & Chr$(13) _
    & Chr$(10) & "Are you sure you want to do this now?", 36, _
    "Restart Windows?") = 6 Then
        x = RestartWindows()
        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:
Hosted by InWind: