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,
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