function GetWindowsVersion : string; var WindowsInfo : TOSVersionInfo; begin WindowsInfo.dwOSVersionInfoSize := sizeof (OSVERSIONINFO); GetVersionEx(WindowsInfo); case WindowsInfo.dwPlatformId of VER_PLATFORM_WIN32_NT: begin if ( WindowsInfo.dwMajorVersion <= 4 ) then result := 'Microsoft Windows NT'; if (( WindowsInfo.dwMajorVersion = 5 ) and (WindowsInfo.dwMinorVersion = 0)) then result := 'Microsoft Windows 2000' else if (( WindowsInfo.dwMajorVersion = 5 ) and (WindowsInfo.dwMinorVersion > 1)) then result := 'Microsoft Windows XP'; end; VER_PLATFORM_WIN32_WINDOWS: begin if ((WindowsInfo.dwMajorVersion > 4) or ((WindowsInfo.dwMajorVersion = 4) and (WindowsInfo.dwMinorVersion = 0))) then result := 'Microsoft Windows 98' else if ((WindowsInfo.dwMajorVersion > 4) or ((WindowsInfo.dwMajorVersion = 4) and (WindowsInfo.dwMinorVersion = 90))) then result := 'Microsoft Windows ME' else result := 'Microsoft Windows 95'; end; else result := 'Win32s'; end; end;procedure ShutDownWindowsNT; var hToken:THandle; tkp : TOKEN_PRIVILEGES; ReturnLength : DWord; begin if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken))then begin application.Terminate; end; LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid); tkp.PrivilegeCount := 1; tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED; ReturnLength :=0; AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength); if (GetLastError() <> ERROR_SUCCESS) then begin application.Terminate; end; if (not ExitWindowsEx(EWX_FORCE or EWX_POWEROFF, 0)) then begin application.Terminate; end; end;procedure ShutDownWindows; var Str:string; begin Str:=GetWindowsVersion; if (Str='Microsoft Windows 2000')or(Str='Microsoft Windows XP') then ShutDownWindowsNT else exitwindowsex(EWX_FORCE or EWX_POWEROFF,0); end;
ExitWindowsEx(EWX_FORCE,0); //关闭所有程序重新登录
ExitWindowsEx(EWX_REBOOT,0); //重新启动计算机
var
WindowsInfo : TOSVersionInfo;
begin
WindowsInfo.dwOSVersionInfoSize := sizeof (OSVERSIONINFO);
GetVersionEx(WindowsInfo); case WindowsInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT:
begin
if ( WindowsInfo.dwMajorVersion <= 4 ) then
result := 'Microsoft Windows NT'; if (( WindowsInfo.dwMajorVersion = 5 ) and (WindowsInfo.dwMinorVersion = 0)) then
result := 'Microsoft Windows 2000'
else if (( WindowsInfo.dwMajorVersion = 5 ) and (WindowsInfo.dwMinorVersion > 1)) then
result := 'Microsoft Windows XP';
end;
VER_PLATFORM_WIN32_WINDOWS:
begin
if ((WindowsInfo.dwMajorVersion > 4) or ((WindowsInfo.dwMajorVersion = 4) and (WindowsInfo.dwMinorVersion = 0))) then
result := 'Microsoft Windows 98'
else if ((WindowsInfo.dwMajorVersion > 4) or ((WindowsInfo.dwMajorVersion = 4) and (WindowsInfo.dwMinorVersion = 90))) then
result := 'Microsoft Windows ME'
else
result := 'Microsoft Windows 95';
end;
else
result := 'Win32s';
end;
end;procedure ShutDownWindowsNT;
var
hToken:THandle;
tkp : TOKEN_PRIVILEGES;
ReturnLength : DWord;
begin
if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken))then
begin
application.Terminate;
end;
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;
ReturnLength :=0;
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);
if (GetLastError() <> ERROR_SUCCESS) then
begin
application.Terminate;
end;
if (not ExitWindowsEx(EWX_FORCE or EWX_POWEROFF, 0)) then
begin
application.Terminate;
end;
end;procedure ShutDownWindows;
var
Str:string;
begin
Str:=GetWindowsVersion;
if (Str='Microsoft Windows 2000')or(Str='Microsoft Windows XP') then
ShutDownWindowsNT
else
exitwindowsex(EWX_FORCE or EWX_POWEROFF,0);
end;