//判断是哪类操作系统,以确定关机方式
function GetOperatingSystem: string;
var
osVerInfo: TOSVersionInfo;
begin
Result := '';
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if GetVersionEx(osVerInfo) then
case osVerInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT:
Result := 'Windows NT/2000/XP';
VER_PLATFORM_WIN32_WINDOWS:
Result := 'Windows 95/98/98SE/Me';
end;
end;
//获得计算机名
function GetComputerName: string;
var
buffer: array[0..MAX_COMPUTERNAME_LENGTH+1] of Char;
Size: Cardinal;
begin
Size := MAX_COMPUTERNAME_LENGTH + 1;
Windows.GetComputerName(@buffer, Size);
Result := strpas(buffer);
end;
//定时关机函数,参数的意义如下:
//Computer:计算机名;Msg:显示的提示信息;Time:时间延迟;Force:是否强制关机;Reboot:是否重启动
function TimedShutDown(Computer: string;
Msg: string;
Time: Word;
Force: Boolean;
Reboot: Boolean): Boolean;
var
rl, hToken: Cardinal;
tkp: TOKEN_PRIVILEGES;
begin
//获得用户关机特权,仅对Windows NT/2000/XP
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
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);
end;
Result:=InitiateSystemShutdown(PChar(Computer), PChar(Msg), Time, Force, Reboot);
end;
//可以在时钟中调用
if GetOperatingSystem = 'Windows NT/2000/XP' then
TimedShutDown(GetComputerName, '系统将要关机!', 30, True, False)
else
ExitWindowsEx(EWX_SHUTDOWN, 0)//安全地关机