1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| procedure FermerWindows(bReboot: boolean);
// bReboot := true => redemarage
// bReboot := false => arret
var
sTokenIn,sTokenOut : TTOKENPRIVILEGES ;
dwLen : DWORD ;
hCurrentProcess,hToken : THANDLE ;
Luid1 : TLargeInteger ;
osVer: OSVERSIONINFO;
begin
try
hCurrentProcess := GetCurrentProcess ;
//ajuste les privilèges, c ce ki est necessaire pour windows XP
OpenProcessToken (hCurrentProcess,TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',Luid1) ;
sTokenIn.PrivilegeCount := 1;
sTokenIn.Privileges[0].Luid := Luid1;
sTokenIn.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,sTokenIn,sizeof(TTOKENPRIVILEGES),
sTokenOut,dwLen);
CloseHandle (hToken);
finally
application.ProcessMessages;
//redemarre
if bReboot then ExitWindowsEx(EWX_REBOOT or EWX_FORCEIFHUNG, 0)
else begin
//arrêt
osVer.dwOSVersionInfoSize := Sizeof(osVer);
GetVersionEx(osVer);
if osVer.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS
//teste la version de windows (9x ou XP)
then ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCEIFHUNG , 0)
else ExitWindowsEx(EWX_POWEROFF or EWX_FORCEIFHUNG , 0);
end;
end;
end; |
Partager