I remember that when I first learned C ++, I liked to study APIs. At that time, my colleague had a master who wrote some code. When I was writing a program, the machine suddenly turned off! When I was wondering, I heard his smile!
It turned out to be his work. Later I studied the InitiateSystemShutdown API function for a long time to understand the working principle, because my machine has joined the Windows domain, in addition, the super user of the domain is also set to have Administrator permissions on my local computer, so he can take advantage of it! Later, I wrote the following code, so that he was remotely shut down while working! I learned new things and turned to others!
// The ShutDownSystem function is used to shut down the local host.
BOOL CAlarmClockDlg: ShutDownSystem ()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
If (! OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, & hToken ))
AfxMessageBox ("OpenProcessToken ");
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME, & tkp. Privileges [0]. Luid );
Tkp. PrivilegeCount = 1; // one privilege to set
Tkp. Privileges [0]. Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges (hToken, FALSE, & tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0 );
If (GetLastError ()! = ERROR_SUCCESS)
AfxMessageBox ("AdjustTokenPrivileges ");
// Shut down the system and force all applications to close.
If (! ExitWindowsEx (EWX_SHUTDOWN | EWX_FORCE, 0 ))
{
Return FALSE;
}
Else
{
Return TRUE;
}
}
// ShutdownHost: This is the C ++ function for remote shutdown! The hostName can be the machine IP address or the machine name!
BOOL CAlarmClockDlg: shutdownHost (CString hostName)
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// Privilege.
If (! OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, & hToken ))
AfxMessageBox ("OpenProcessToken failed .");
// Get the LUID for shutdown privilege.
LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME, & tkp. Privileges [0]. Luid );
Tkp. PrivilegeCount = 1; // one privilege to set
Tkp. Privileges [0]. Attributes = SE_PRIVILEGE_ENABLED;
// Get shutdown privilege for this process.
AdjustTokenPrivileges (hToken, FALSE, & tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0 );
// Cannot test the return value of AdjustTokenPrivileges.
If (GetLastError ()! = ERROR_SUCCESS)
AfxMessageBox ("AdjustTokenPrivileges enable failed .");
// Display the shutdown dialog box and start the time-out countdown.
FResult = InitiateSystemShutdown ("192.168.100.245", // shut down local computer
"Click on the main window and press the Escape key to cancel shutdown.", // message to user
1, // time-out period
FALSE, // ask user to close apps // pay attention to this API call!
FALSE); // reboot after shutdown
If (! FResult)
{
AfxMessageBox ("InitiateSystemShutdown failed .");
}
// Disable shutdown privilege.
Tkp. Privileges [0]. Attributes = 0;
AdjustTokenPrivileges (hToken, FALSE, & tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0 );
If (GetLastError ()! = ERROR_SUCCESS)
{
AfxMessageBox ("AdjustTokenPrivileges disable failed .");
}
Return TRUE;
}