After the application is compiled, we need to design the packaging and distribution functions of the application. When compiling the installation program, you often encounter the following problem: after the program is installed, You need to restart the computer. In the code, how does one control Windows shutdown or restart?
To implement this function, you need to use an ExitWindowsEx function provided by Windows API. You can view the Help file provided by C ++ Builder to obtain the usage format of this function:
BOOL ExitWindowsEx (
UINT uFlags,
// Set shutdown Parameters
DWORD dwReserved
// System reserved words
);
Description of uFlags parameters:
EWX_FORCE forces the program to terminate and exit the system
EWX_LOGOFF logs out and logs in as another user
EWX_REBOOT restart
EWX_SHUTDOWN
DwReserved is a reserved word of the system. Enter 0 at will.
After learning how to use the ExitWindowsEx function, the following example describes how to control Windows shutdown or restart in C ++ Builder.
The design steps are as follows:
1. After running the C ++ Builder system, activate the "File/New Application" menu to create a New project.
2. Activate the "File/Save As" menu to Save the form unit As the File name exitu. cpp, and then activate the "File/Save Project As" menu to Save the Project File As the File name expri. bpr. (Note: The project file name and form unit file name must not have the same name .)
3. add four TButton components Button1, Button2, Button3, and Button4 to the form, set their Caption attributes to "other identity Logon", "shutdown", "restart", and "Force shutdown ".
4. Set the OnClick event response code of the Button1, Button2, Button3, and Button4 components respectively as follows:
Void _ fastcall TForm1: Button1Click (TObject Sender) {// other identity logon DWORD temp; ExitWindowsEx (EWX_LOGOFF, temp);} // your void _ fastcall TForm1 :: button2Click (TObject Sender) {// shut down DWORD temp; ExitWindowsEx (EWX_SHUTDOWN, temp);} // define void _ fastcall TForm1: Button3Click (TObject Sender) {// restart DWORD temp; ExitWindowsEx (EWX_REBOOT, temp);} // ------------------------------ void _ fastcall TForm1: Button4Click (TObject Sender) {// Force Shutdown DWORD temp; exitWindowsEx (EWX_FORCE, temp );}
|
5. After running the program, click the four buttons to test their functions. If you can complete various settings, it means you have succeeded, then we can apply the methods described in this article to the self-developed C ++ Builder application to control Windows shutdown.