Solved

WM_POWERBROADCAST

Posted on 2007-03-17
7
1,218 Views
Last Modified: 2008-01-09
Hi experts.
I need to intercept the power button press. Searching in msdn...I found that WM_POWERBROADCAST is responsable for this shutdown operation.
http://msdn2.microsoft.com/en-us/library/aa373148.aspx
Can anyone help me use it?
Thanks a lot.
0
Comment
Question by:A. Cristian Csiki
7 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 18742997
it's actually pretty straitforward:

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

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

end.


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

Expert Comment

by:2266180
ID: 18743008
remove the
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
(it was a rip from another project and I forgot to remove those lines :D )
0
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18744229
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?

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:saravananvg
ID: 18746869
0
 
LVL 28

Expert Comment

by:2266180
ID: 18746911
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.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18747288
>>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.

ziolko.
0
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18750861
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?
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now