Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Pasar la PC a estado de Hibernación (hibernar) (https://www.clubdelphi.com/foros/showthread.php?t=80817)

cHackAll 23-07-2007 03:58:57

Pasar la PC a estado de Hibernación (hibernar)
 
Código Delphi [-]
uses Windows;
// Requiere:
//   Panel de Control -> Hibernación (Habilitar hibernación).
//   Al igual que ExitWindowsEx, requiere habilitar el privilegio SE_SHUTDOWN_NAME

var
 hToken, Dummy: Cardinal;
 Privileges: packed record
  Count: Cardinal;
  Luid: Int64;
  Attributes: Cardinal;
 end = (Count: 1; Attributes: SE_PRIVILEGE_ENABLED);

begin
 if OpenProcessToken(DWORD(-1), TOKEN_ADJUST_PRIVILEGES, hToken) then                                    // Abrimos la "ficha" de privilegios de nuestro proceso para hacer una modificación,
  begin
   LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Privileges.Luid);                                    // Obtenemos el identificador del privilegio SE_SHUTDOWN_NAME
   AdjustTokenPrivileges(hToken, False, PTokenPrivileges(@Privileges)^, SizeOf(Privileges), nil, Dummy); // Habilitamos mediante el ID obtenido dicho privilegio...
   CloseHandle(hToken);                                                                                  // Cerramos el manejador
  end;

 SetSystemPowerState(False, True);                                                                       // Hibernamos...
end.

function SetSystemPowerState(fSuspend, fForce: LongBool): LongBool;
// Al estar fSuspend fijado como Verdadero suspenderá el equipo, caso contrario intentará hibernarlo.
// Cuando dicha API es llamada; envía a todas las ventanas e hilos que procesen mensajes, un mensaje notificando la accion tomada.
// Al estar fForce fijado como True, simplemente hace una notificación. Caso contrario solicita a "todos" el permiso de hacerlo.

cHackAll 24-01-2008 18:45:43

Optimizado;

Código Delphi [-]
uses Windows;

function IsPwrHibernateAllowed: LongBool; external 'powrprof.dll';

var hToken: Cardinal;

begin
 if OpenProcessToken(DWORD(-1), TOKEN_ADJUST_PRIVILEGES, hToken) then
  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 not IsPwrHibernateAllowed then
  MessageBox(0, 'No está habilitada la hibernación del sistema!', nil, MB_ICONEXCLAMATION)
 else if not SetSystemPowerState(False, True) then
  MessageBox(0, 'No se ha podido hibernar el equipo!', nil, MB_ICONEXCLAMATION);
end.

Saludos

cHackAll 21-11-2008 17:42:09

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
  {Log.Write('Unable to perform hibernation');};
end;

cHackAll 28-11-2008 20:37:29

-IsPwrHibernateAllowed may be altered or unavailable in newer versions of Windows
+GetPwrCapabilities determines if the system supports sleep state S4 and if the feature is enabled
+ForceCritical hibernates the system immediately on XP, W3K & W2K
-user should manage the return Value (!MessageBox)

Código Delphi [-]
function GetPwrCapabilities(var SystemPowerCapabilities): LongBool; stdcall 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 Hibernate(ForceCritical: LongBool = False): Integer;
var
 SystemPowerCapabilities: array [0..63] of Boolean;
 hToken: Cardinal;
begin
 Result := 5{>0}; // GetPwrCapabilities; GetLastError
 if GetPwrCapabilities(SystemPowerCapabilities) then
  if not SystemPowerCapabilities[6]{S4} then
   Result := 3 // ACPI; El equipo no admite o no es compatible con la hibernación!
  else if not SystemPowerCapabilities[8]{hiberfil.sys} and (CallNtPowerInformation(10, @Result, 1, nil, 0) <> 0) then
   Result := 1 // SeCreatePagefilePrivilege; No tiene los suficientes privilegios para habilitar la hibernación del equipo!
  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
     Result := 2 // SeShutdownPrivilege; No tiene los suficientes privilegios para hibernar el equipo!
    else if not SetSuspendState(True, ForceCritical, True) then // WM_POWERBROADCAST ~ SetThreadExecutionState
     Result := 4 // Error; No se pudo hibernar el equipo!
    else
     Result := 0;
    CloseHandle(hToken);
   end;
end;

Regards :D


La franja horaria es GMT +2. Ahora son las 15:55:19.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi