Código Delphi
[-]function IsPwrHibernateAllowed: LongBool; external 'powrprof';
function SetSuspendState(Hibernate, ForceCritical, DisableWakeEvent: LongBool): LongBool; stdcall external 'powrprof';
function CallNtPowerInformation(InformationLevel: Cardinal; lpInputBuffer: Pointer; nInputBufferSize: Cardinal; lpOutputBuffer: Pointer; nOutputBufferSize: Cardinal): Cardinal; stdcall external 'powrprof';
function ForceHibernation: Boolean;
const uType = MB_TOPMOST or MB_ICONEXCLAMATION;
var hToken: Cardinal;
begin
Result := True;
if not IsPwrHibernateAllowed and (CallNtPowerInformation(10, @Result, 1, nil, 0) <> 0) then
MessageBox(0, 'No se pudo activar la hibernación del equipo!', nil, uType)
else
begin
OpenProcessToken(DWORD(-1), TOKEN_ADJUST_PRIVILEGES, hToken);
AdjustTokenPrivileges(hToken, False, PTokenPrivileges(PChar(#1#0#0#0#19#0#0#0#0#0#0#0#2#0#0#0))^, 16, nil, PDWORD(0)^);
if GetLastError <> 0 then
MessageBox(0, 'No tiene los suficientes privilegios para hibernar el quipo!', nil, uType)
else if not SetSuspendState(True, True, True) then
MessageBox(0, 'No se pudo hibernar el equipo!', nil, uType)
else
Result := False;
CloseHandle(hToken);
end;
Result := not Result;
end;
uso:
Código Delphi
[-]procedure TForm1.Button1Click(Sender: TObject);
begin
if not ForceHibernation then
;
end;