Qt calls Windows API for shutdown

Source: Internet
Author: User

In the past, I did not know how to shut down at a time, so I used QT to write a program for implementation.

There are two key points: 1. Get the time difference from the scheduled start to the present

2. How to disable a computer

You can use Windows API to disable a computer. Here, we need to install windows. h of mingw \ include in the QT creator directory. If the VC compiler is used, use Windows. h in VC.

The Code is as follows. You can call this function to disable the computer function.

1 # include "windows. h" 2 3 bool shutdown: mysystemshutdown () 4 {5 handle htoken; 6 token_privileges tkp; 7 8 // get process flag 9 If (! Openprocesstoken (getcurrentprocess (), token_adjust_privileges | token_query, & htoken) 10 return false; 11 12 // luid13 lookupprivilegevalue (null, se_shutdown_name, & tkp. privileges [0]. luid); 14 tkp. privilegecount = 1; 15 tkp. privileges [0]. attributes = se_privilege_enabled; 16 17 // get the shutdown privilege of this process 18 adjusttokenprivileges (htoken, false, & tkp, 0, (ptoken_privileges) null, 0 ); 19 if (getlasterror ()! = Error_success) return false; 20 21 // force disable computer 22 if (! Exitwindowsex (ewx_shutdown | ewx_force, 0) 23 return false; 24 return true; 25}
Related Article

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.