function OpenThread(dwDesiredAccess: Longword; bInheritHandle: BOOL; dwThreadId: Longword): THandle; stdcall; external 'Kernel32.dll';
function ShutdownSystem(bForce: Boolean): Boolean;
var
Handle: THandle;
Priveleges: TOKEN_PRIVILEGES;
Size: Longword;
luid: int64;
begin
Result := False;
if not OpenThreadToken(OpenThread(PROCESS_ALL_ACCESS, False, GetCurrentThreadId()), TOKEN_ALL_ACCESS, True, Handle) and not OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, Handle) then
Exit;
if not LookupPrivilegeValue(nil, 'SeShutdownPrivilege', luid) then
Exit;
Priveleges.PrivilegeCount := 1;
Priveleges.Privileges[0].Luid := luid;
Priveleges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not AdjustTokenPrivileges(Handle, False, Priveleges, SizeOf(Priveleges), Priveleges, Size) then
Exit;
if bForce then
Result := ExitWindowsEx(EWX_FORCE or EWX_POWEROFF, 0) or ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)
else
Result := ExitWindowsEx(EWX_POWEROFF, 0) or ExitWindowsEx(EWX_SHUTDOWN, 0)
end;
int main(int argc, char **argv)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
//-------------------
char *name=""; // address of name of computer to shut down
char *msg=""; //address of message to display in dialog box
DWORD time=0; // time to display dialog box
bool force=true; // force applications with unsaved changes flag
bool reboot=true; //reboot flag
//---------------------------------------------------------------------
function OpenThread(dwDesiredAccess: Longword; bInheritHandle: BOOL; dwThreadId: Longword): THandle; stdcall; external 'Kernel32.dll';
function ShutdownSystem(bForce: Boolean): Boolean;
var
Handle: THandle;
Priveleges: TOKEN_PRIVILEGES;
Size: Longword;
luid: int64;
begin
Result := False;
if not OpenThreadToken(OpenThread(PROCESS_ALL_ACCESS, False, GetCurrentThreadId()), TOKEN_ALL_ACCESS, True, Handle) and not OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, Handle) then
Exit;
if not LookupPrivilegeValue(nil, 'SeShutdownPrivilege', luid) then
Exit;
Priveleges.PrivilegeCount := 1;
Priveleges.Privileges[0].Luid := luid;
Priveleges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not AdjustTokenPrivileges(Handle, False, Priveleges, SizeOf(Priveleges), Priveleges, Size) then
Exit;
if bForce then
Result := ExitWindowsEx(EWX_FORCE or EWX_POWEROFF, 0) or ExitWindowsEx(EWX_FORCE or EWX_SHUTDOWN, 0)
else
Result := ExitWindowsEx(EWX_POWEROFF, 0) or ExitWindowsEx(EWX_SHUTDOWN, 0)
end;