StandBy und Hibernate (Ruhezustand) aktivieren(Tipp drucken)



Da das Programm auf eine grafische Oberfläche verzichtet, ist es unter Delphi 5 nur 17KB gross. Nach dem Packen mit UPX sogar nur 10KB. Sie können die Anwendung und den Quellcode frei verwenden.

StandBy.exe /s    aktiviert den StandBy-Mode sofern unterstützt
StandBy.exe /h    aktiviert den Hibernate-Mode (Ruhezustand), sofern unterstützt

StandBy.zip
StandBy.exe

program StandBy;

uses
  Windows;

var
  IsHibernate: boolean = false;
  IsStandBy: boolean = false;

function IsWinNT: boolean;
var
  OSVersion: TOSVersionInfo;
begin
  Result := false;
  OSVersion.dwOSVersionInfoSize := sizeof(TOSVersionInfo);
  if GetVersionEx(OSVersion) then
    Result := (OSVersion.dwPlatformId = VER_PLATFORM_WIN32_NT);
end;

procedure CheckSystemPowerState;
type
  TIsCheckFkt = function: boolean; stdcall;
var
  hDll: THandle;
  IsCheckFkt: TIsCheckFkt;
begin
  hDll := LoadLibrary('PowrProf.dll');
  if hDll <> 0 then
  begin
    @IsCheckFkt := GetProcAddress(hDll, 'IsPwrHibernateAllowed');
    if assigned(IsCheckFkt) then
      IsHibernate := IsCheckFkt;
    @IsCheckFkt := GetProcAddress(hDll, 'IsPwrSuspendAllowed');
    if assigned(IsCheckFkt) then
      IsStandBy := IsCheckFkt;
  end;
end;

procedure SetStandBy(SetStandBy: boolean);
var
  TokenPriv: TTokenPrivileges;
  TokenHandle: THandle;
  CurrentProc: THandle;
  Len: Cardinal;
begin
  CheckSystemPowerState;

  // WinNT, 2000, XP need the SE_SHUTDOWN_NAME privilege
  if IsWinNT then
  begin
    // determine the actual Process
    CurrentProc := GetCurrentProcess;

    // get process token for privileges
    if OpenProcessToken(CurrentProc, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
                        TokenHandle) then
    begin
      // returns the system-id of the privilege
      if LookupPrivilegeValue(nil, 'SeShutdownPrivilege',
                              TokenPriv.Privileges[0].LUID) then
      begin
        TokenPriv.PrivilegeCount := 1;
        TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

        // here y try to get the privilege
        if AdjustTokenPrivileges(TokenHandle, False, TokenPriv, 0, nil, Len) then
        begin
          if (SetStandBy = false) and IsHibernate then
            // and then y can got to StandBy or Hibernate 
            // (first parameter = True => StandBy, False => Hibernate)
            SetSystemPowerState(SetStandBy, False);
          if SetStandBy and IsStandBy then
            // and then y can got to StandBy or Hibernate
            // (first parameter = True => StandBy, False => Hibernate)
            SetSystemPowerState(SetStandBy, False);
        end;
      end;
    end;
  end
  else
    if IsStandBy then
      // Win95/98/Me dont need Privileges and dont support hibernating
      SetSystemPowerState(False, False);
end;

begin
  if ParamCount = 1 then
  begin
    if ParamStr(1) = '/h' then
      SetStandBy(False)
    else if ParamStr(1) = '/s' then
      SetStandBy(True);
  end;
end.