Koda za shutsown:
{*****************************************************************************************
*
* Unit Name : mpShutdown
* Purpose : shutdown, reboot, poweroff, logoff Windows
* Author : markus stephany
* Coppyright: (C) 2001 markus stephany:
This source code is freeware. You may use, change, and distribute without
charge the source code as you like. This unit can be used freely in any
commercial applications. However, it may not be sold as a standalone product
and the source code may not be included in a commercial product. This unit
is provided as is with no warrent or support. Make sure to read relevant
information and documentation from Microsoft before using this unit.
* Version : 0.01
* Date : 24.04.2001 19:51:23
* History : -
*
function SysDown(bForce: Boolean=False): Boolean
shuts down the computer and eventually powers it down (if apm is working)
function SysReboot(bForce: Boolean=False): Boolean
restarts the computer
function ShutdownWindows(eKind: TShutdown; const bForce:Boolean = False): Boolean;
generic shutdown/logoff/reboot function
*****************************************************************************************}
unit mpShutdown;
interface
uses
Windows, SysUtils;
type
(* kind of shutdown *)
TShutDown = (sdPowerOff, sdReboot, sdShutdown, sdLogoff);
(* ShutdownWindows: ----------------------------------------------------------
generic shutdown/logoff/reboot function
eKind: kind of shutdown
bForce: if True, force termination of applications (possible data loss!!)
returns: True on success, False on failure
------------------------------------------------------------------------------*)
(* SysDown: ------------------------------------------------------------------
shuts down the computer and eventually powers it down (if apm is working)
bForce: if True, force termination of applications (possible data loss!!)
returns: True on success, False on failure
------------------------------------------------------------------------------*)
(* SysReboot: ----------------------------------------------------------------
restarts the computer
bForce: if True, force termination of applications (possible data loss!!)
returns: True on success, False on failure
------------------------------------------------------------------------------*)
(* SwitchPrivilege: ----------------------------------------------------------
-privilege function for nt/w2k
sPrivilege: name of the privilege
bSet: if True, enable the privilege, otherwise disable the privilege
returns: True on success, False on failure
------------------------------------------------------------------------------*)
function ShutdownWindows(eKind: TShutdown; const bForce:Boolean = False): Boolean;
function SysDown(bForce: Boolean = False): Boolean;
function SysReboot(bForce: Boolean = False): Boolean;
function SwitchPrivilege(sPrivilege: string; bSet: Boolean): Boolean;
implementation
const
(* name of the shutdown privilege (needed on nt) *)
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
function SwitchPrivilege(sPrivilege: string; bSet: Boolean): Boolean;
var
(* process token *)
hToken: THandle;
(* privilege storage *)
pTokPriv: PTokenPrivileges;
const
(* enable/disable *)
iAttrs: Array[Boolean] of Integer = (0,SE_PRIVILEGE_ENABLED);
begin
(* assume failure *)
Result := False;
(* open process token *)
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
(* get memory for privilege data *)
GetMem(pTokPriv, SizeOf(TTOKENPRIVILEGES) + SizeOf(TLUIDANDATTRIBUTES));
try
(* get current state *)
if LookupPrivilegeValue(NIL, PChar(sPrivilege), pTokPriv^.Privileges[0].Luid) then
begin
(* one priv to modify *)
pTokPriv^.PrivilegeCount := 1;
(* set enabled/disabled *)
pTokPriv^.Privileges[0].Attributes := iAttrs[bSet];
(* set new state *)
Result:= AdjustTokenPrivileges(hToken, FALSE, pTokPriv^, 0, TTokenPrivileges(nil^), DWORD(nil^));
end;
finally
(* free the privilege buffer *)
FreeMem(pTokPriv);
end;
end;
end;
function ShutdownWindows(eKind: TShutdown; const bForce:Boolean = False): Boolean;
const
(* ExitWindowsEx consts for eKind *)
cEWXFlags: array [TShutdown] of Cardinal = (
EWX_POWEROFF,
EWX_REBOOT,
EWX_SHUTDOWN,
EWX_LOGOFF
);
var
cFlags: Cardinal;
begin
(* assume failure *)
Result := False;
(* prepare ewx-flag *)
cFlags := cEWXFlags[eKind];
if bForce
then
cFlags := cFlags or EWX_FORCE;
(* on nt/w2k, the SE_SHUTDOWN privilege must be set *)
if Win32Platform = VER_PLATFORM_WIN32_NT
then
if not SwitchPrivilege(SE_SHUTDOWN_NAME,True)
then
Exit;
(* do the actual ewx call *)
Result := ExitWindowsEx(cFlags,0);
(* on nt/w2k, disable the SE_SHUTDOWN privilege *)
if Win32Platform = VER_PLATFORM_WIN32_NT
then
SwitchPrivilege(SE_SHUTDOWN_NAME,False);
end;
function SysDown(bForce: Boolean = False): Boolean;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT
then
(* EWX_POWEROFF seems to work on nt/w2k only *)
Result := ShutdownWindows(sdPowerOff, bForce) or ShutdownWindows(sdShutdown, bForce)
else
(* win9x seems to poweroff without the EWX_POWEROFF flag (it doesn't seem to to work there either) *)
Result := ShutdownWindows(sdShutdown, bForce);
end;
function SysReboot(bForce: Boolean = False): Boolean;
begin
(* simply do reboot *)
Result := ShutdownWindows(sdReboot, bForce)
end;
end.
|