My application needs to know when the system is shutting down. Is there any API or an alternative way to determine if the user has initiated System Shutdown?
Printable View
My application needs to know when the system is shutting down. Is there any API or an alternative way to determine if the user has initiated System Shutdown?
Use the UnloadMode parameter of the QueryUnload event:
HTH,Code:Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then
'user is exiting windows...
End If
End Sub
Duncan
For future reference, here are all of the unloadmodes that you could useVB Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 'there are 5 unloadmode levels Select Case UnloadMode Case vbFormControlMenu 'UnloadMode 0 'form is being unloaded via the Close 'command from the System menu Case vbFormCode 'UnloadMode 1 'Unload Me has been issued from code Case vbAppWindows 'UnloadMode 2 'Windows itself is closing Case vbAppTaskManager 'UnloadMode 3 'the Task Manager is closing the app Case vbFormMDIForm 'UnloadMod 4 'an MDI child form is closing because 'its parent form is closing End Select End Sub
I have not been able to get QueryUnload or Unload to work in Win2k when the User presses the Power Button on a ATX Power Supply.
Does ANYONE know the API routine to capture windows messages and detect when the operating system is shutting down?
My program losses data because I can't detect when it is shutting down.
Now if the user goes to Start > Shutdown > Shutdown then there is no problem.
Anyone with Win2k and ATX Power you will notice when you press the Power Button the computer shutdowns like three times fasters then going through the start button.
Megatron could you maybe help? I have posted this question on Microsoft Public VB WinAPI Newsgroup and no one has the answer.
Eagerly awaiting reply.
Thanx guys this would be of a great help for now...I wonder how I just missed that....
The problem that ALFware is having is part of my problem too since i`m actually using Win2k proffessional so a little help on that too might be useful to my little program....
I am glad that someone else has run into this issue
I have posted it so many times at the Microsoft newsgroups but none of them have come back with anything useful.
Any Gurus out there that know the solution?
Thanks
Subclass your window and catch the WM_QUERYENDSESSION message. This message is sent to all windows when windows is about to end the session.
Megatron: Does WM_QUERYENDSESSION apply to all OS, across the board? (Even XP)?
All except for Windows CE.
Also isn't the Form_QueryUnload() just a wrap for that anyway?
Megatron is it possible for you to post the Subclassing example for this exact situation? I have used subclassing before and it worked on Win98 and WinXP but for some reason in Win2k when you press the ATX Power button it didn't send the message.
Megatron if you have a newish computer and running windows 2k make a program to capture this message and get it to log to a text file when the computer shuts down. Then run it and use the power button to shut down the computer. You should find that windows does it's normal saving settings... and closing stuff then shuts off. However I notice that using the power button to shutdown takes about 50% less time that the Start > Shutdown method?
Is there any difference? wouldn't the power button just be a "shortcut" for shutdown?
If you could post a working sample I would love to double check my original findings.
Thanks Megatron
Yes, but WM_QUERYENDSESSION allows you to distinguish between a shutdown and a log-off.Quote:
Also isn't the Form_QueryUnload() just a wrap for that anyway?
I'm not sure, but I think it's because when you press power-off (instead of shutdown), windows will only do the minimum requirements.Quote:
However I notice that using the power button to shutdown takes about 50% less time that the Start > Shutdown method?
Would it be possible to send me a working example project that subclasses the form and does something when the query comes through.
Thanks megatron
This should work
Add to a module
Add to a formVB Code:
Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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 Const GWL_WNDPROC = (-4) Public WinProcOld As Long Private Const ENDSESSION_LOGOFF = &H80000000 Private Const WM_QUERYENDSESSION = &H11 Public Function WinProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long If wMsg = WM_QUERYENDSESSION Then If wParam = ENDSESSION_LOGOFF Then Debug.Print "logging off" Else Debug.Print "Shutting down" End If WinProc = CallWindowProc(WinProcOld&, hwnd&, wMsg&, wParam&, lParam&) End Function Sub SubClassWnd(hwnd As Long) WinProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WinProc) End Sub Sub UnSubclassWnd(hwnd As Long) SetWindowLong hwnd, GWL_WNDPROC, WinProcOld& WinProcOld& = 0 End Sub
VB Code:
Private Sub Form_Load() SubClassWnd hwnd End Sub Private Sub Form_Unload(Cancel As Integer) UnSubclassWnd hwnd End Sub
Also megatron how can I get a copy of that VBFE Library, sounds interesting and rather useful but can't seem to find a link to download it.
The VBFE library was cancelled a couple years ago (I didn't have the time to finish it).