|
-
Apr 17th, 2001, 09:26 AM
#1
Thread Starter
Frenzied Member
You need to subclass your application's main window and wathc for a WM_POWER message.
If you recieve that message and the wParam is PWR_SUSPENDREQUEST then return PWR_FAIL and don't pass the message on to the default window proc then the power suspend will not go ahead.
There are some good examples at Merrion Computing
-
Apr 17th, 2001, 10:09 AM
#2
Hyperactive Member
how do I watch for the powerrequest message
How do I watch for it to come and what is the exact syntax on sending back a fail message. Thank you for the help
Joe
-
Apr 17th, 2001, 11:01 AM
#3
Thread Starter
Frenzied Member
Warning: complex code
OK - first you need a function with which to replace (aka Subclassing) the window's default message function:
Public OldProcAddress As Long
'\\ --[VB_WindowProc]--------------------------------------------
'\\ 'typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
'\\ Parameters:
'\\ hwnd - window handle receiving message
'\\ wMsg - The window message (WM_..etc.)
'\\ wParam - First message parameter
'\\ lParam - Second message parameter
'\\ Note:
'\\ When subclassing a window proc using this, set the eventhandler's
'\\ hOldWndProc property to the window's previous window proc address.
'\\ --------------------------------------------------------------------
'\\ You have a royalty free right to use, reproduce, modify, publish and mess with this code
'\\ I'd like you to visit http://www.merrioncomputing.com for updates, but won't force you
'\\ -------------------------------------------------------------------
Public Function VB_WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Local Error Resume Next
Dim lRet As Long
'\\ If its a power suspending broadcast, kill it...
If wMsg = WM_POWER And wParam = PWR_SUSPENDREQUEST Then
VB_WindowProc = PWR_FAIL
Else
VB_WindowProc = CallWindowProc(OldProcAddress, hwnd, wMsg, wParam, lParam)
End If
End Function
Then when your main form loads, substitute this window proc fro the one already there.
Public Sub Form_Load()
OldProcAddress = SetWindowLong(Me.Hwnd, GWL_WNDPROC, AddressOf VB_WindowProc)
End Sub
And don't fdorget to undo this subclassing before closing your app's main form.
Private Sub Form_Unload()
Call SetWindowLong(Me.Hwnd, GWL_WNDPROC, OldProcAddress)
End Sub
Hope this helps,
Duncan
There are articles and more source code, including a drop-in subclassing control at
Merrion Computing Downloads
-
Apr 17th, 2001, 01:03 PM
#4
Hyperactive Member
Still one question....
I am still pretty new to api and your code makes some sense but I am still missing one very important aspect. How do I access the function you made. Do I access it within the timer, adn if so what values do I use. Thank you very much for your help and patience.
Joe
-
Apr 17th, 2001, 02:17 PM
#5
Duncan, unlike C++, VB does not have the constants automatically declared for you, thus you need to declare them yourself.
The following are the 2 constants needed (Since PWR_SUSPENDREQUEST is 1, it's really not necessary to declare it, but declaring it can avoid confusion)
Code:
Private Const WM_POWER = &H48
Private Const PWR_SUSPENDREQUEST = 1
Joey_k29: the code will replace the default window procedure, so it will run the instant to call it (in the load event).
Also make sure to close your program using the X in the corner (not the stop button), otherwise it will crash.
-
Apr 17th, 2001, 02:34 PM
#6
Hyperactive Member
Thank you Megatron for the additional help...
Do I also need to include the constant for pwr_fail as well as GWL_WNDPROC? If not then I am still eithering doing some wrong, or the code is not ME compliant. It is needed for 95, but will it work in 98/NT/ME? Maybe that is my problem at this point. Thank you both for all your help.
Literally the code I am using that pertains to suspension:
Module1.mod:
Public OldProcAddress As Long
Private Const WM_POWER = &H48
Private Const PWR_SUSPENDREQUEST = 1
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Function VB_WindowProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Local Error Resume Next
Dim lRet As Long
'\\ If its a power suspending broadcast, kill it...
If wMsg = WM_POWER And wParam = PWR_SUSPENDREQUEST Then
VB_WindowProc = PWR_FAIL
Else
VB_WindowProc = CallWindowProc(OldProcAddress, hWnd, wMsg, wParam, lParam)
End If
End Function
Form1:
Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(Me.hWnd, GWL_WNDPROC, OldProcAddress)
End Sub
Private Sub Form_Load()
OldProcAddress = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf VB_WindowProc)
end Sub
Sincerely,
Joe
Last edited by Joey_k29; Apr 17th, 2001 at 02:45 PM.
-
Apr 17th, 2001, 02:44 PM
#7
Yes, you'd need them too.
Code:
Private Const GWL_WNDPROC = (-4)
Private Const PWR_FAIL = (-1)
-
Apr 17th, 2001, 03:36 PM
#8
Hyperactive Member
I added the code...
It does not work in windows ME or 98. I have not been able to try 95 yet. I am using standby in ME and 98 not suspend. Would that account for the code not working?
Thanks again for your help,
Joe
Last edited by Joey_k29; Apr 17th, 2001 at 06:51 PM.
-
Apr 18th, 2001, 04:45 AM
#9
Thread Starter
Frenzied Member
Unfortunately, this is one of the areas in which Windows NT/2000 and Windows 9x are diveregent.
The full code that will work for both is:
'\\ In a .BAS file
Option Explicit
Public oldProcAddress As Long
Public Enum enPowerBroadcastType
PBT_APMQUERYSUSPEND = &H0
PBT_APMQUERYSTANDBY = &H1
PBT_APMQUERYSUSPENDFAILED = &H2
PBT_APMQUERYSTANDBYFAILED = &H3
PBT_APMSUSPEND = &H4
PBT_APMSTANDBY = &H5
PBT_APMRESUMECRITICAL = &H6
PBT_APMRESUMESUSPEND = &H7
PBT_APMRESUMESTANDBY = &H8
End Enum
Public Const BROADCAST_QUERY_DENY = &H424D5144
Public Const WM_POWER = &H48
Public Const WM_POWERBROADCAST = &H218
Public Const PWR_SUSPENDREQUEST = 1
Public Const GWL_WNDPROC = (-4)
Public Const PWR_FAIL = (-1)
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'\\ --[VB_WindowProc]--------------------------------------------
'\\ 'typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
'\\ Parameters:
'\\ hwnd - window handle receiving message
'\\ wMsg - The window message (WM_..etc.)
'\\ wParam - First message parameter
'\\ lParam - Second message parameter
'\\ Note:
'\\ When subclassing a window proc using this, set the eventhandler's
'\\ hOldWndProc property to the window's previous window proc address.
'\\ --------------------------------------------------------------------
'\\ You have a royalty free right to use, reproduce, modify, publish and mess with this code
'\\ I'd like you to visit http://www.merrioncomputing.com for updates, but won't force you
'\\ -------------------------------------------------------------------
Public Function VB_WindowProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
On Local Error Resume Next
Dim lRet As Long
'\\ If its a power suspending broadcast, kill it...
If wMsg = WM_POWER And wParam = PWR_SUSPENDREQUEST Then
'\\ This is the message in Windows NT/2000
VB_WindowProc = PWR_FAIL
ElseIf wMsg = WM_POWERBROADCAST And wParam = PBT_APMQUERYSUSPEND Then
VB_WindowProc = BROADCAST_QUERY_DENY
Else
VB_WindowProc = CallWindowProc(oldProcAddress, hWnd, wMsg, wParam, lParam)
End If
End Function
Then add the following to your applicatiuon's main form:
Private Sub Form_Load()
oldProcAddress = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf VB_WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(Me.hWnd, GWL_WNDPROC, oldProcAddress)
End Sub
HTH,
Duncan
http://www.merrioncomputing.com
-
Apr 18th, 2001, 07:31 AM
#10
Hyperactive Member
EXCELLENT!
Merrion you have been extremely helpful. The code is working excellently. Within a month I'll actually know what it means too . I can't thank you enough for your help and patience. Thanks to you megatron too for your contribution. My next post will have to be addressed to you both. Thank you again.
Tranquilly,
Joe
-
Apr 18th, 2001, 08:17 AM
#11
Thread Starter
Frenzied Member
Unfortunately subclassing is an extremely difficult and complicated process.
With this in mind I have written some articles on what is actually being done here at http://www.merrioncomputing.com and am also working on a very ambitious project to provide a class wrapper for this and many other parts of the Windows API such that you can have am object model for the operating system in exactly the same manner as you have for the various Microsoft Office apps.
If you would like to look at the articles or the early Beta of the code, please drop by.
HTH,
Duncan
-
Apr 18th, 2001, 08:29 AM
#12
Hyperactive Member
Really??
That would simplify tremendously the process. I am very interested in your success with it. I am currently reading an API book, which I hope I can finish in a little over a month. By the end of the learning curve I should be versed well enough to understand much of what you have given and are currently programming. Thank you again for you help, and if you wish to contact me with any information you have I can be reached at [email protected].
Good Luck with your project,
Joe
-
Apr 18th, 2001, 08:32 PM
#13
Hyperactive Member
I spoke too soon....
As I should have imagined, just because the code works in ME and 98 does not mean it works in 95, and as my luck has proved when something can go wrong, it will. Sure enough, the code does not work in windows 95. Any ideas??? This ordeal has been incredibly frustrating I hope you can put an end to it. Thank you again. I hope you can solve this problem as well.
Frustrated,
Joe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|