Hi experts.
I need to intercept the power button press. Searching in msdn...I found that WM_POWERBROADCAST is responsable for this shutdown operation.
Can anyone help me use it?
Thanks a lot.
LVL 11
A. Cristian CsikiSenior System AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it's actually pretty straitforward:

unit Unit1;


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

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    { Private declarations }
    procedure WMPowerBroadcast(var Msg: TMessage); message WM_POWERBROADCAST;
    { Public declarations }

  Form1: TForm1;


{$R *.dfm}

procedure TForm1.WMPowerBroadcast(var Msg: TMessage);
  showmessage('good night ');


with this code, you will get a "good night" message before the system goes into suspend and anotehr "good night" message when it returns.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
remove the
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
(it was a rip from another project and I forgot to remove those lines :D )
A. Cristian CsikiSenior System AdministratorAuthor Commented:
Hi ciuly.
Glad once again you help me. I tried checking your code, but nothing happens. I tried even to put a memo and save the WMPowerBroadcast msg in a log file from that procedure but still nothing.
One more thing. If I press the power button or I go Start-SHutdown-SHutdown is the same msg that receives WMPowerBroadcast?

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

if you don't get that message, the first thing that gets into my mind is the following quote from MSDN: "Client      Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0." (from here: http://msdn2.microsoft.com/en-us/library/aa373247.aspx)
are you running from one of these?

"If I press the power button or I go Start-SHutdown-SHutdown is the same msg that receives WMPowerBroadcast?"
no. that message is WM_EndSession.
>>WM_POWERBROADCAST is responsable for this shutdown operation

WM_QUERYENDSESSION and WM_ENDSESSION are sent when shutting down
according to MSDN:
"The WM_POWERBROADCAST message is broadcast to an application to notify it of power-management events" if you look at possible wParam values you'll see that this message is sent when computer is about to resume/suspend or is switching from battery power to A/C.

A. Cristian CsikiSenior System AdministratorAuthor Commented:
HI all.
Got the point with  WM_QUERYENDSESSION and WM_POWERBROADCAST. I'm running win xp sp2 with delphi 7 pro. Still can't trap the power button press, or the shutdown caused by it.

I even trieded a component found on www.CTVNet.ch, but not working. I don't know what I miss....

Any ideas?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.