Control of Windows shutdown in CB program

Source: Internet
Author: User
Tags exit response code

After the application is completed, we need to design the packaging and distribution capabilities of the application. When you write setup, you often experience the problem that you need to restart your computer after the program is installed. So how do you control the shutdown or reboot of Windows in your code?

To implement this functionality, you need to use a exitwindowsex function provided by the Windows API. We can get the use format of this function by looking at the help file provided by C + + Builder:

BOOL ExitWindowsEx(
   UINT uFlags, // 设置关机参数
   DWORD dwReserved // 系统保留字
   );

The main setting values and significance of uflags are shown in table 1.

Dwreserved is the system reserved word, casually fill in a 0 can.

After understanding the use format of the ExitWindowsEx function, here is an example of how to control Windows shutdown or restart in C + + Builder.

The design steps are as follows:

1. After running the C + + Builder system, first activate the menu "File/new application" to create a new project.

2. Activate the menu "File/save as" to save the form cell with the filename exitu.cpp, and then activate the menu "File/save project as" to save the project file as a file name EXITP.BPR. (Note: The project file name does not have the same name as the form cell file name.) )

3. Add four TButton components Button1, Button2, Button3, and Button4 on the form, and set their caption properties as "other login", "Shutdown", "reboot" and "Force shutdown" respectively.

4. The OnClick event response code for the Button1, Button2, Button3, and Button4 components is set as follows:

  void __fastcall TForm1::Button1Click(TObject Sender)
   {
   //其它身份登录
   DWORD temp;
   ExitWindowsEx(EWX_LOGOFF,temp);
   }
   //------------------------------
   void __fastcall TForm1::Button2Click(TObject Sender)
   {
   //关机
   DWORD temp;
   ExitWindowsEx(EWX_SHUTDOWN,temp);
   }
   //------------------------------
   void __fastcall TForm1::Button3Click(TObject Sender)
   {
   //重新启动
   DWORD temp;
   ExitWindowsEx(EWX_REBOOT,temp);
   }
   //------------------------------
   void __fastcall TForm1::Button4Click(TObject Sender)
   {
   //强制关机
   DWORD temp;
   ExitWindowsEx(EWX_FORCE,temp);
   }

Table 1 Setting value brief meaning

Ewx_force Force terminate program to run and exit system

Ewx_logoff exit and log in as a different user

Ewx_reboot reboot

Ewx_shutdown shutdown

5. After running the program, click on four buttons to test their function. If you can complete the functions of various settings, you have succeeded, then you can use the method described in this article to your own developed C + + Builder application to control the shutdown of Windows.

This program runs in Chinese Windows 98, C + + Builder 5.0 environment.

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.