Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 23-07-2007
Avatar de cHackAll
[cHackAll] cHackAll is offline
Baneado?
 
Registrado: oct 2006
Posts: 2.159
Poder: 20
cHackAll Va por buen camino
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.
Responder Con Cita
  #2  
Antiguo 24-01-2008
Avatar de cHackAll
[cHackAll] cHackAll is offline
Baneado?
 
Registrado: oct 2006
Posts: 2.159
Poder: 20
cHackAll Va por buen camino
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
Responder Con Cita
  #3  
Antiguo 21-11-2008
Avatar de cHackAll
[cHackAll] cHackAll is offline
Baneado?
 
Registrado: oct 2006
Posts: 2.159
Poder: 20
cHackAll Va por buen camino
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;
Responder Con Cita
  #4  
Antiguo 28-11-2008
Avatar de cHackAll
[cHackAll] cHackAll is offline
Baneado?
 
Registrado: oct 2006
Posts: 2.159
Poder: 20
cHackAll Va por buen camino
-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
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 16:51:00.


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
Copyright 1996-2007 Club Delphi