Practical Skills for controlling Windows shutdown in C ++

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.