Talking about the timing shutdown of the system

Source: Internet
Author: User

Before I wrote this program, I had seen a shutdown program written by someone else in the SDK. However, his program is only valid under the Win9x, in 2000 is not, can not shut down the machine can only log off. Because 2000 is a multi-user operating system, you will be granted permission to shut down. After reviewing MSDN, the shutdown program was sorted out.Token_privileges TKP;
HANDLE Htoken;

if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges | Token_query, &htoken))
MessageBox ("OpenProcessToken failed!");
Lookupprivilegevalue (NULL, SE_SHUTDOWN_NAME,&TKP. Privileges[0].   LUID); Get the local machine unique identity
TKP. Privilegecount = 1;
TKP. Privileges[0]. Attributes = se_privilege_enabled;
AdjustTokenPrivileges (Htoken, FALSE, &TKP, 0, (ptoken_privileges) NULL, 0); Adjust the permissions you get

if (GetLastError ()!= ERROR_SUCCESS)
MessageBox ("adjusttokenprivileges enable failed!");
Fresult =initiatesystemshutdown (
NULL,//To shut down the computer user name, you can turn off each other's machine in the LAN network, NULL means Guan Ben machine
"Because the system is not stable, Windows will be shut down in the above time, please do save work!",//message displayed
10,//Shutdown time required
TRUE); Set to True for start, set to False for shutdown
if (!fresult)
MessageBox ("InitiateSystemShutdown failed.");
TKP. Privileges[0]. Attributes = 0;
AdjustTokenPrivileges (Htoken, FALSE, &TKP, 0, (ptoken_privileges) NULL, 0);
if (GetLastError ()!= ERROR_SUCCESS)
MessageBox ("adjusttokenprivileges disable failed.");
ExitWindowsEx (ewx_shutdown,0); Start off the machine.
Figure one is the program running screen:

Figure I

See the source code for specific implementation details. WIN2000 + vc++6.0 by compiling.

This article supporting source code

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: 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.