Here is the source code to shut down the computer for Windows NT/2000/XP
Code:
#include "windows.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
TOKEN_PRIVILEGES tkp;
HANDLE hToken;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hToken); //We now have a Token for this Process.
//Now we must find the LUID ###'s for Shutting down the computer in Windows NT.
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //Should now have the LUID Stored in our Privilege.
//Must set the privilege count.
tkp.PrivilegeCount = 1; //We are only setting one privilege for now.
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //Set this Privilege to Enabled. Otherwise it would disable it.
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,NULL,0);
if (GetLastError() != ERROR_SUCCESS)
{
cout<<"Failed To Set Privileges";
}
ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, NULL);
if (GetLastError() != ERROR_SUCCESS)
{
cout<<"Failed to shut down computer using forced attributes.";
}
return 0;
}
The attributes are not forced, but you can add EWX_FORCE constant to force the shutdown of applications.