Einzelnen Beitrag anzeigen

schwa226

Registriert seit: 4. Apr 2008
400 Beiträge
 
#5

Re: Standby zurück wird nicht erkannt?

  Alt 30. Okt 2009, 19:34
Hallo,

jetzt muss ich diesen alten Post nocheinmal rauskramen.

Nun habe ich festgestellt das ich Probleme habe mit erkennen ob der PC in den Standby geht.

Ich habe ein externes Testprogramm, da geht es:
Zitat:
19:03:44 Running on:
19:03:44 Windows 7 Ultimate Edition x86 Version: 6.1.7600

19:03:44 for further details look: http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx

19:10:40 Msg: WM_POWERBROADCAST
19:10:40 WParam: PBT_APMSUSPEND, Info: System is suspending operation.

19:11:53 Msg: WM_POWERBROADCAST
19:11:53 WParam: PBT_APMRESUMEAUTOMATIC, Info: Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.

19:11:53 Msg: WM_POWERBROADCAST
19:11:53 WParam: PBT_APMRESUMESUSPEND, Info: Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.
Der Code ist hier aus dem Board.

Nun habe ich ein DLL-Plugin das auch auf die Power Message reagieren soll.
Also habe ich der DLL eine Form hinzugefügt um auf die WM_POWERBROADCAST reagieren zu können:
Zitat:
19:11:56 Msg: WM_POWERBROADCAST
19:11:56 WParam: PBT_APMRESUMEAUTOMATIC, Info: Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.

19:11:56 Msg: WM_POWERBROADCAST
19:11:56 WParam: PBT_APMRESUMESUSPEND, Info: Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.
Hier fehlt die Meldung PBT_APMSUSPEND??

Beide Codes laufen zur selben Zeit auf dem selben System - trotzdem erkennt meine DLL nicht das der PC in den Ruhezustand geht?

Hier der Code von der Form in der DLL:
Delphi-Quellcode:
unit PowerWatch;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, INIFiles, StdCtrls;

type
  TFormPowerWatch = class(TForm)
    Memo1: TMemo;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure WMPowerBroadcast(var MyMessage: TMessage); message WM_POWERBROADCAST;
  end;

var
  FormPowerWatch: TFormPowerWatch;

const
  PBT_APMQUERYSUSPEND = $0000;
  PBT_APMQUERYSTANDBY = $0001;

  PBT_APMQUERYSUSPENDFAILED = $0002;
  PBT_APMQUERYSTANDBYFAILED = $0003;

  PBT_APMSUSPEND = $0004;
  PBT_APMSTANDBY = $0005;

  PBT_APMRESUMECRITICAL = $0006;
  PBT_APMRESUMESUSPEND = $0007;
  PBT_APMRESUMESTANDBY = $0008;

  PBTF_APMRESUMEFROMFAILURE = $00000001;

  PBT_APMBATTERYLOW = $0009;
  PBT_APMPOWERSTATUSCHANGE = $000A;
  PBT_POWERSETTINGCHANGE = $8013;

  PBT_APMOEMEVENT = $000B;
  PBT_APMRESUMEAUTOMATIC = $0012;


implementation

{$R *.dfm}

function TranslatePowerBroadcast(const Msg: Cardinal): string;
begin
  case Msg of
    WM_POWERBROADCAST: Result := 'WM_POWERBROADCAST';

    PBT_APMQUERYSUSPEND: Result := 'PBT_APMQUERYSUSPEND, Info: Request for permission to suspend. In Windows Server 2008 and Windows Vista, use the SetThreadExecutionState function instead.';
    PBT_APMQUERYSTANDBY: Result := 'PBT_APMQUERYSTANDBY';

    PBT_APMQUERYSUSPENDFAILED: Result := 'PBT_APMQUERYSUSPENDFAILED';
    PBT_APMQUERYSTANDBYFAILED: Result := 'PBT_APMQUERYSTANDBYFAILE, Info: Suspension request denied. In Windows Server 2008 and Windows Vista, use SetThreadExecutionState instead.D';

    PBT_APMSUSPEND: Result := 'PBT_APMSUSPEND, Info: System is suspending operation.';
    PBT_APMSTANDBY: Result := 'PBT_APMSTANDBY';

    PBT_APMRESUMECRITICAL: Result := 'PBT_APMRESUMECRITICAL, Info: Operation resuming after critical suspension. In Windows Server 2008 and Windows Vista, use PBT_APMRESUMEAUTOMATIC instead.';
    PBT_APMRESUMESUSPEND: Result := 'PBT_APMRESUMESUSPEND, Info: Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.';
    PBT_APMRESUMESTANDBY: Result := 'PBT_APMRESUMESTANDBY';

    //PBTF_APMRESUMEFROMFAILURE: Result := 'PBTF_APMRESUMEFROMFAILURE';

    PBT_APMBATTERYLOW: Result := 'PBT_APMBATTERYLOW, Info: Battery power is low. In Windows Server 2008 and Windows Vista, use PBT_APMPOWERSTATUSCHANGE instead.';
    PBT_APMPOWERSTATUSCHANGE: Result := 'PBT_APMPOWERSTATUSCHANGE, Info: Power status has changed.';
    //PBT_POWERSETTINGCHANGE: Result := 'PBT_POWERSETTINGCHANGE, Info: A power setting change event has been received.';

    PBT_APMOEMEVENT: Result := 'PBT_APMOEMEVENT, Info: OEM-defined event occurred. In Windows Server 2008 and Windows Vista, this event is not available because these operating systems support only ACPI; APM BIOS events are not supported.';
    PBT_APMRESUMEAUTOMATIC: Result := 'PBT_APMRESUMEAUTOMATIC, Info: Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.';
  else
    Result := 'Unknown Message "'+IntToStr(Msg)+'"';
  end;
end;

procedure TFormPowerWatch.WMPowerBroadcast(var MyMessage: TMessage);
var
  IniFile : TIniFile;
begin

case MyMessage.Msg of

   WM_POWERBROADCAST: begin
    // windows powermanagement message

    Memo1.Lines.Append(TimeToStr(now) + ' Msg: '+TranslatePowerBroadcast(MyMessage.Msg));
    Memo1.Lines.Append(TimeToStr(now) + ' WParam: '+TranslatePowerBroadcast(MyMessage.WParam));
    Memo1.Lines.Append('');

    case MyMessage.WParam of

      PBT_APMSUSPEND, PBT_APMSTANDBY,
      PBT_APMQUERYSUSPEND, PBT_APMQUERYSTANDBY: begin

        // Hier hin, was getan werden muss, bevor Windows in den Standby darf,
        // z.B. Netzwerk- oder Datenbankverbindungen trennen, Timer abstellen, etc.
        // Seit Vista hat man hier nur noch max. 2 Sekunden Zeit was zu erledigen.

        MyMessage.Result := BROADCAST_QUERY_DENY; // Standby/Ruhezustand verweigern
      end;

      PBT_APMRESUMECRITICAL, PBT_APMRESUMESUSPEND,
      PBT_APMRESUMESTANDBY, PBT_APMRESUMEAUTOMATIC: begin

        // Windows kommt aus dem Standby/Ruhezustand wieder.
        // Hier z.B. Verbindungen wiederherstellen.

        MyMessage.Result := 1; // Standby/Ruhezustand erlauben

      end;
    end;
   end;
end;

end;

end.
Und hier der Code mit dem es geht:
Delphi-Quellcode:
unit FormMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, WindowsVersionInfo;

type
  TMainForm = class(TForm)
    MemoMessageLog: TMemo;
    WindowsVersionInfo: TWindowsVersionInfo;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    procedure WMPowerBroadcast(var MyMessage: TMessage); message WM_POWERBROADCAST;
  end;

var
  MainForm: TMainForm;

const
  PBT_APMQUERYSUSPEND = $0000;
  PBT_APMQUERYSTANDBY = $0001;

  PBT_APMQUERYSUSPENDFAILED = $0002;
  PBT_APMQUERYSTANDBYFAILED = $0003;

  PBT_APMSUSPEND = $0004;
  PBT_APMSTANDBY = $0005;

  PBT_APMRESUMECRITICAL = $0006;
  PBT_APMRESUMESUSPEND = $0007;
  PBT_APMRESUMESTANDBY = $0008;

  PBTF_APMRESUMEFROMFAILURE = $00000001;

  PBT_APMBATTERYLOW = $0009;
  PBT_APMPOWERSTATUSCHANGE = $000A;
  PBT_POWERSETTINGCHANGE = $8013;

  PBT_APMOEMEVENT = $000B;
  PBT_APMRESUMEAUTOMATIC = $0012;

implementation

{$R *.dfm}

function TranslatePowerBroadcast(const Msg: Cardinal): string;
begin
  case Msg of
    WM_POWERBROADCAST: Result := 'WM_POWERBROADCAST';

    PBT_APMQUERYSUSPEND: Result := 'PBT_APMQUERYSUSPEND, Info: Request for permission to suspend. In Windows Server 2008 and Windows Vista, use the SetThreadExecutionState function instead.';
    PBT_APMQUERYSTANDBY: Result := 'PBT_APMQUERYSTANDBY';

    PBT_APMQUERYSUSPENDFAILED: Result := 'PBT_APMQUERYSUSPENDFAILED';
    PBT_APMQUERYSTANDBYFAILED: Result := 'PBT_APMQUERYSTANDBYFAILE, Info: Suspension request denied. In Windows Server 2008 and Windows Vista, use SetThreadExecutionState instead.D';

    PBT_APMSUSPEND: Result := 'PBT_APMSUSPEND, Info: System is suspending operation.';
    PBT_APMSTANDBY: Result := 'PBT_APMSTANDBY';

    PBT_APMRESUMECRITICAL: Result := 'PBT_APMRESUMECRITICAL, Info: Operation resuming after critical suspension. In Windows Server 2008 and Windows Vista, use PBT_APMRESUMEAUTOMATIC instead.';
    PBT_APMRESUMESUSPEND: Result := 'PBT_APMRESUMESUSPEND, Info: Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.';
    PBT_APMRESUMESTANDBY: Result := 'PBT_APMRESUMESTANDBY';

    //PBTF_APMRESUMEFROMFAILURE: Result := 'PBTF_APMRESUMEFROMFAILURE';

    PBT_APMBATTERYLOW: Result := 'PBT_APMBATTERYLOW, Info: Battery power is low. In Windows Server 2008 and Windows Vista, use PBT_APMPOWERSTATUSCHANGE instead.';
    PBT_APMPOWERSTATUSCHANGE: Result := 'PBT_APMPOWERSTATUSCHANGE, Info: Power status has changed.';
    PBT_POWERSETTINGCHANGE: Result := 'PBT_POWERSETTINGCHANGE, Info: A power setting change event has been received.';

    PBT_APMOEMEVENT: Result := 'PBT_APMOEMEVENT, Info: OEM-defined event occurred. In Windows Server 2008 and Windows Vista, this event is not available because these operating systems support only ACPI; APM BIOS events are not supported.';
    PBT_APMRESUMEAUTOMATIC: Result := 'PBT_APMRESUMEAUTOMATIC, Info: Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.';
  else
    Result := 'Unknown Message "'+IntToStr(Msg)+'"';
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
var
  wvi : TWindowsVersionInfo;
begin

  wvi := TWindowsVersionInfo.Create(self);

  MemoMessageLog.Lines.Append(TimeToStr(now) + ' Running on:');
  MemoMessageLog.Lines.Append(TimeToStr(now) + ' ' + wvi.WindowsVersionsString + ' ' + wvi.ServicePack +
    ' Version: ' + IntToStr(wvi.MajorVersion) + '.' + IntToStr(wvi.MinorVersion ) + '.' + IntToStr(wvi.BuildNumber));
  MemoMessageLog.Lines.Append('');
  MemoMessageLog.Lines.Append(TimeToStr(now) + ' for further details look: http://msdn.microsoft.com/en-us/library/aa373247(VS.85).aspx');
  MemoMessageLog.Lines.Append('');
end;

procedure TMainForm.WMPowerBroadcast(var MyMessage: TMessage);
begin
  if MyMessage.Msg = WM_POWERBROADCAST then begin
    MemoMessageLog.Lines.Append(TimeToStr(now) + ' Msg: '+TranslatePowerBroadcast(MyMessage.Msg));
    MemoMessageLog.Lines.Append(TimeToStr(now) + ' WParam: '+TranslatePowerBroadcast(MyMessage.WParam));
    MemoMessageLog.Lines.Append('');
    //MemoMessageLog.Lines.SaveToFile(ExtractFileDir(Application.ExeName)+'\Message.log');

    // windows powermanagement message
    if (MyMessage.WParam = PBT_APMQUERYSUSPEND) or
       (MyMessage.WParam = PBT_APMQUERYSTANDBY)
    then begin
      // windows wants to go into standby or hibernation mode

      // Hier hin, was getan werden muss, bevor Windows in den Standby darf,
      // z.B. Netzwerk- oder Datenbankverbindungen trennen, Timer abstellen, etc.

      MyMessage.Result := 1; // allow standby/hibernation
      // MyMessage.Result := BROADCAST_QUERY_DENY; // deny standby/hibernation

    end else if (MyMessage.WParam = PBT_APMRESUMECRITICAL) or
                (MyMessage.WParam = PBT_APMRESUMESUSPEND) or
                (MyMessage.WParam = PBT_APMRESUMESTANDBY)
    then begin
      // windows returns from standby or hibernation

      // Hier z.B. Verbindungen wiederherstellen
    end;
  end;
end;

end.
Ich kann hier keinen Unterschied finden! Oder gibt es da Unterschiede wenn eine DLL mit Form verwendet wird?
Die Form wird beim laden des Plugins so erzeugt:
Delphi-Quellcode:
      if not Assigned(FormPowerWatch) then
          FormPowerWatch := TFormPowerWatch.Create(nil);
Delphi 2010, Update 4 & 5
  Mit Zitat antworten Zitat