There has been alot of discussion on the C/C++ boards, as well as this one, about how to restart the system (or shutdown).
I posted this code in response to a thread on the C++ board, but not everybody cross reads posts so I think I'll post it here -
The following is a simple program that opens a dialog and gives you a few seconds before the system shuts down. The code can be edited to abort shutdown within the time allowed, the code of which can be found here
Code:
#include "stdafx.h"
#include "windows.h"
BOOL fResult;
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
MessageBox(NULL,NULL,"OpenProcessToken failed.", MB_OK | MB_ICONINFORMATION);
// 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)
MessageBox(NULL,NULL,"AdjustTokenPrivileges enable failed.",MB_OK | MB_ICONINFORMATION);
// Display the shutdown dialog box and start the time-out countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
"Click on the main window and press the Escape key to cancel shutdown.", // message to user
3, // time-out period
TRUE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
{
MessageBox(NULL,NULL,"InitiateSystemShutdown failed.",MB_OK | MB_ICONINFORMATION);
}
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
{
MessageBox(NULL,NULL,"AdjustTokenPrivileges disable failed.",MB_OK | MB_ICONINFORMATION);
}
return 0;
}
Enjoy. Just don't use this code for evil.