|
|
|
Visual
Basic Restart Windows
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
' Causes Windows to Restart
' If any programs refuse to terminate, then this function will
return a ZERO
Dim i As Integer
Dim EW_RESTARTWINDOWS As Long
EW_RESTARTWINDOWS = &H42
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 - since it will do what it says it will do. 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!
|
|