Learning Process of C ++ remote shutdown API

Source: Internet
Author: User

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;
}

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.