Results 1 to 15 of 15

Thread: System Shutdown

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Cool System Shutdown

    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?
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Use the UnloadMode parameter of the QueryUnload event:

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
    If UnloadMode = vbAppWindows Then
      'user is exiting windows...
    End If
    
    End Sub
    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    For future reference, here are all of the unloadmodes that you could use
    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2. 'there are 5 unloadmode levels
    3.         Select Case UnloadMode
    4.             Case vbFormControlMenu 'UnloadMode 0
    5.                  'form is being unloaded via the Close
    6.                  'command from the System menu
    7.             Case vbFormCode        'UnloadMode 1
    8.                  'Unload Me has been issued from code
    9.             Case vbAppWindows      'UnloadMode 2
    10.                  'Windows itself is closing
    11.             Case vbAppTaskManager  'UnloadMode 3
    12.                  'the Task Manager is closing the app
    13.             Case vbFormMDIForm     'UnloadMod 4
    14.                  'an MDI child form is closing because
    15.                  'its parent form is closing
    16.         End Select
    17. End Sub

  4. #4
    ALFWare
    Guest

    Thumbs down QueryUnload DOES NOT WORK FOR WIN2k

    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.

  5. #5

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378
    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....
    Last edited by vbud; Jan 18th, 2002 at 01:17 AM.
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  6. #6
    ALFWare
    Guest

    Any luck?

    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

  7. #7
    Megatron
    Guest
    Subclass your window and catch the WM_QUERYENDSESSION message. This message is sent to all windows when windows is about to end the session.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Megatron: Does WM_QUERYENDSESSION apply to all OS, across the board? (Even XP)?

  9. #9
    Megatron
    Guest
    All except for Windows CE.

  10. #10
    ALFWare
    Guest

    I'm sure I did that...

    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

  11. #11
    Megatron
    Guest
    Also isn't the Form_QueryUnload() just a wrap for that anyway?
    Yes, but WM_QUERYENDSESSION allows you to distinguish between a shutdown and a log-off.
    However I notice that using the power button to shutdown takes about 50% less time that the Start > Shutdown method?
    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.

  12. #12
    ALFWare
    Guest

    How about an example?

    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

  13. #13
    Megatron
    Guest
    This should work

    Add to a module
    VB Code:
    1. Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
    2. 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
    3. Public Const GWL_WNDPROC = (-4)
    4. Public WinProcOld As Long
    5. Private Const ENDSESSION_LOGOFF = &H80000000
    6. Private Const WM_QUERYENDSESSION = &H11
    7.  
    8. Public Function WinProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    9.     If wMsg = WM_QUERYENDSESSION Then
    10.         If wParam = ENDSESSION_LOGOFF Then
    11.             Debug.Print "logging off"
    12.         Else
    13.             Debug.Print "Shutting down"
    14.         End If
    15.     WinProc = CallWindowProc(WinProcOld&, hwnd&, wMsg&, wParam&, lParam&)
    16. End Function
    17.  
    18. Sub SubClassWnd(hwnd As Long)
    19.     WinProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WinProc)
    20. End Sub
    21.  
    22. Sub UnSubclassWnd(hwnd As Long)
    23.     SetWindowLong hwnd, GWL_WNDPROC, WinProcOld&
    24.     WinProcOld& = 0
    25. End Sub
    Add to a form
    VB Code:
    1. Private Sub Form_Load()
    2.     SubClassWnd hwnd
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.     UnSubclassWnd hwnd
    7. End Sub

  14. #14
    ALFWare
    Guest

    Will give it a go...

    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.

  15. #15
    Megatron
    Guest
    The VBFE library was cancelled a couple years ago (I didn't have the time to finish it).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width