procedure TForm1.EnablePrivileges;
begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
raise Exception.Create('OpenProcessToken failed.');
PrivilName := 'SeShutdownPrivilege';
if not LookupPrivilegeValue(Nil, PrivilName, tkp.Privileges[0].Luid) then
raise Exception.Create('No rights to continue');
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), tkp_prev, ReturnLength);
if GetLastError <> ERROR_SUCCESS then
raise Exception.Create('AdjustTokenPrivileges enable failed.');
end;
procedure TForm1.DisablePrivileges;
begin
tkp.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), tkp_prev, ReturnLength);
if GetLastError <> ERROR_SUCCESS then
raise Exception.Create('AdjustTokenPrivileges disable failed.');
end;
procedure TForm1.shutDown();
begin
if OsCur='95' then
ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0)
else
begin
EnablePrivileges;
if not InitiateSystemShutdown(Nil,'Save your documents right away, system is about to shutdown',5,True,False) then
raise Exception.Create('InitiateSystemShutdown failed.');
DisablePrivileges;
end;
end;
Voilà
Ensuite il faut changer EWX_SHUTDOWN en EWX_REBOOT,
appeler la procedure dans l'évenenemnt Form1.onClose() et la tour est joué !
Précision:
je pense que changer EWX_SHUTDOWN en EWX_REBOOT ne marche que pour les anciens OS (95?) maintenant il faut mettre true comme dernier paramètre dans le InitiateSystemShutdown