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.