In the Visual Basic program, you can use Windows API functions to restart the computer system. This text describes how to exit Windows 95 and shut down the computer system.
#Shut down the computer system
You can use the ExitWindowsEx function of the Windows API to restart the computer system from a Visual Basic program. To use this function, include the following declaration statement in the normal declarations section of the form:
PRivateDeclareFunctionExitWindowsExLib"user32"(ByVal
uFlagsAsLong,ByValdwReservedAsLong)AsLong
The ExitWindowsEx function requires 2 parameters. You can use one or more combinations of the following flags to tell the ExitWindowsEx function that you want to perform the shutdown process.
EWX_FORCE All processes are forcibly terminated.
EWX_LOGOFF All processes are forcibly terminated and the user is logged off (loggedoff).
EWX_POWEROFF The computer system is shut down, and if power saving features are supported, the computer is physically shut down.
EWX_REBOOTThe computer system was shut down and restarted.
EWX_SHUTDOWN The computer was physically and safely shut down.
The following sample program uses a combination of the above three flags. This combination of flags (EWX_LOGOFF, EWX_FORCE, and EWX_REBOOT) tells Windows95 to exit all normal
During the execution process, the user logs out of the network and puts the computer system in a ready state for the user to shut down.
#Sample program
This sample program shows how to shut down a computer system.
1. Start a new project in Visual Basic and use the default method to create Form1.
2. Add the following constants and declaration statements to the normal declaration section of Form1 (note that the declaration statement needs to be written in one line):
PrivateDeclareFunctionExitWindowsExLib"user32"(ByVal
uFlagsAsLong,ByValdwReservedAsLong)AsLong
ConstEWX_LOGOFF=0
ConstEWX_SHUTDOWN=1
ConstEWX_REBOOT=2
ConstEWX_FORCE=4
ConstEWX_POWEROFF=8
ConstEWX_RESET=EWX_LOGOFF EWX_FORCE EWX_REBOOT
3. Add a command button control to Form1 and use the default method to create Comand1. Set its Caption property to "Restart computer."
4. Add the following code to the click event of Command1:
PrivateSubCommand1_Click()
DimXAsLong
X=ExitWindowsEx(EWX_RESET,dwReserved)
EndSub
Press F5 to run the above sample program and click the command button to restart the computer system. ->