unit Unit2;
interface
uses
Windows,
SysUtils;
const
SHUTDOWN_FORCE_OTHERS = $00000001;
SHUTDOWN_FORCE_SELF = $00000002;
SHUTDOWN_GRACE_OVERRIDE = $00000020;
SHUTDOWN_HYBRID = $00000200;
SHUTDOWN_INSTALL_UPDATES = $00000040;
SHUTDOWN_NOREBOOT = $00000010;
SHUTDOWN_POWEROFF = $00000008;
SHUTDOWN_RESTART = $00000004;
SHUTDOWN_RESTARTAPPS = $00000080;
function WindowsShutDown(Computer: PChar; Msg: PChar; Time: Word; Force: Boolean; Reboot: Boolean): Boolean;
function InitiateShutdown(lpMachineName, lpMessage: PWideChar; dwGracePeriode, dwShutdownFlags, dwReason: DWORD): DWORD;
stdcall;
{$EXTERNALSYM InitiateShutdown}
function InitiateShutdownA(lpMachineName, lpMessage: PAnsiChar; dwGracePeriode, dwShutdownFlags, dwReason: DWORD): DWORD;
stdcall;
{$EXTERNALSYM InitiateShutdownA}
function InitiateShutdownW(lpMachineName, lpMessage: PWideChar; dwGracePeriode, dwShutdownFlags, dwReason: DWORD): DWORD;
stdcall;
{$EXTERNALSYM InitiateShutdownW}
implementation
function InitiateShutdown;
external advapi32
name {$IFDEF UNICODE}'
InitiateShutdownW'
{$ELSE}'
InitiateShutdownA'
{$ENDIF};
function InitiateShutdownA;
external advapi32
name '
InitiateShutdownA';
function InitiateShutdownW;
external advapi32
name '
InitiateShutdownW';
function WindowsShutDown(Computer: PChar; Msg: PChar; Time: Word; Force: Boolean; Reboot: Boolean): Boolean;
var
rl: Cardinal;
hToken: Cardinal;
tkp: TOKEN_PRIVILEGES;
flags: DWORD;
begin
Result:=False;
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
or TOKEN_QUERY, hToken)
then
RaiseLastOSError
else
begin
if LookupPrivilegeValue(
nil, '
SeShutdownPrivilege', tkp.Privileges[0].Luid)
then
begin
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1;
AdjustTokenPrivileges(hToken, False, tkp, 0,
nil, rl);
if GetLastError <> ERROR_SUCCESS
then
RaiseLastOSError
else
begin
if Win32MajorVersion >= 6
then
begin
//Flags
if Reboot
then
flags := SHUTDOWN_FORCE_SELF
or SHUTDOWN_GRACE_OVERRIDE
or SHUTDOWN_RESTART
else
flags := SHUTDOWN_FORCE_SELF
or SHUTDOWN_GRACE_OVERRIDE
or SHUTDOWN_INSTALL_UPDATES;
//Befehl ausführen
if InitiateShutdown(Computer, Msg, Time, flags, 0) = ERROR_SUCCESS
then
result := True
else
RaiseLastOSError;
end
else
begin
if InitiateSystemShutdown(Computer, Msg, Time, Force, Reboot)
then
result := True
else
RaiseLastOSError;
end;
{else}
end;
{else}
end
else
RaiseLastOSError;
end;
{else}
end;
end.