Intercept power broadcast messages: How do you tell if Bindevent is really successful? SendMessage!
Question:
BINDEVENT( ) always returns 1 when a Windows message event binding is created. Soooo, how do you know it was actually successful? If I always get a 1 from BindEvent() and AEVENTS() is always populated, all regardless of success, how do I know I'm successfully bound?
In the case of a bind to something like SHCNE_CREATE (respond to file/folder creation), I could invisibly create/delete a DIR and see if my handler delegate event fires, but with something like a WM_POWERBROADCAST/PBT_APMQUERYSUSPEND combo, you obviously can't do that transparently.
How do you test if the event binding really works?
SendMessage is your friend. It’s a way to call your windows message procedure directly with parameters you specify.
The sample below hooks power broadcast events from the OS. Notice the SendMessage call that pretends the user is putting Windows into Standby mode.
(You could also use Spy to watch windows messages.)
(see also the sample that ships with VFP: Tools->Task Pane->Solution Samples->New in VFP9->Binding to Windows Message Events->Power Broadcast Events)
CLEAR
#define GWL_WNDPROC (-4)
#define WM_QUERYENDSESSION 0x0011
#define WM_POWERBROADCAST 0x0218
#define PBT_APMQUERYSUSPEND 0x0000
#define PBT_APMQUERYSTANDBY 0x0001
#define PBT_APMQUERYSUSPENDFAILED 0x0002
#define PBT_APMQUERYSTANDBYFAILED 0x0003
#define PBT_APMSUSPEND 0x0004
#define PBT_APMSTANDBY 0x0005
#define PBT_APMRESUMECRITICAL 0x0006
#define PBT_APMRESUMESUSPEND 0x0007
#define PBT_APMRESUMESTANDBY 0x0008
#define PBTF_APMRESUMEFROMFAILURE 0x00000001
#define PBT_APMBATTERYLOW 0x0009
#define PBT_APMPOWERSTATUSCHANGE 0x000A
#define PBT_APMOEMEVENT 0x000B
#define PBT_APMRESUMEAUTOMATIC 0x0012
#define BROADCAST_QUERY_DENY 0x424D5144 &&// Return this value to deny a query.("BMQD")
PUBLIC oPowerHandler
oPowerHandler=NEWOBJECT("PowerHandler")
DECLARE integer SendMessage IN WIN32API ;
integer hWnd,integer Msg,;
integer wParam,;
integer lParam
?SendMessage(_vfp.hWnd,WM_POWERBROADCAST,PBT_APMQUERYSUSPEND,0)
DEFINE CLASS PowerHandler AS session
dwOrigWindProc=0
PROCEDURE init
DECLARE integer GetWindowLong IN WIN32API ;
integer hWnd, ;
integer nIndex
DECLARE integer CallWindowProc IN WIN32API ;
integer lpPrevWndFunc, ;
integer hWnd,integer Msg,;
integer wParam,;
integer lParam
THIS.dwOrigWindProc =GetWindowLong(_VFP.HWnd,GWL_WNDPROC)
BINDEVENT(0, WM_QUERYENDSESSION,this,"HandleMsg")
BINDEVENT(_VFP.hWnd, WM_POWERBROADCAST,this,"HandleMsg")
?"Monitoring power"
PROCEDURE HandleMsg(hWnd as Integer, msg as Integer, wParam as Integer, lParam as Integer)
LOCAL nRetvalue
nRetvalue=0
DO case
CASE msg=WM_QUERYENDSESSION
?PROGRAM(),hwnd,GetWIndowTitle(hwnd)
IF MESSAGEBOX("End Session?",4+32+4096,"Fox Rox!")=6 && 6 = yes
nRetvalue= 1 && 0 means don't allow quit&&CallWindowProc(this.dwOrigWindProc ,hWnd,msg,wParam,lParam)
ELSE
nRetvalue=0
ENDIF
RETURN nRetvalue
CASE msg=WM_POWERBROADCAST
?"WM_POWERBROADCAST",SECONDS(),hWnd,msg,wParam,lParam
DO CASE
CASE wParam=PBT_APMQUERYSUSPEND
IF MESSAGEBOX("'Stand by' machine?",4+32+4096,"Fox Rox!")=6
nRetvalue=CallWindowProc(this.dwOrigWindProc,hWnd,msg,wParam,lParam)
ELSE
nRetvalue=BROADCAST_QUERY_DENY && "BMQD"
ENDIF
OTHERWISE
nRetvalue=CallWindowProc(this.dwOrigWindProc,hWnd,msg,wParam,lParam)
ENDCASE
OTHERWISE
nRetvalue= CallWindowProc(this.dwOrigWindProc ,hWnd,msg,wParam,lParam)
ENDCASE
RETURN nRetvalue
ENDDEFINE
49190
Comments
- Anonymous
January 24, 2005
This question was from TrevorH at Microsoft - Anonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=calvin-hsia-s-weblog-intercept-power-broadcast-messages-how-do-you