Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent windows shutdown

Status
Not open for further replies.

VbDev100

Programmer
Nov 9, 2012
3
IN
I want my application to be always running. I have included password so that if someone tries to close the application, it will ask for password. Now I am trying to prevent windows from shutting down, restart or logging off while my application is running. Whenever it happens, my application will flash some message. I tried MsgHook component (MsgHoo32.OCX) from Link to intercept windows message WM_QUERYENDSESSION

I have 2 problems.

1. In the WM_QUERYENDSESSION description it is mentioned that the application may return FALSE to cancel shutdown. How can I return a value while using MsgHook.

2. I used following code without returning any value. It works, but some other programs (mostly in system tray) are still unloaded. After that my program shows "Shutting Down" message & then shut down is cancelled.

Code:
Private Const WM_QUERYENDSESSION = &H11
Private Const ENDSESSION_LOGOFF As Long = &H80000000
Private Const WM_CANCELMODE = &H1F

Private Sub Form_Load()
    Msghook1.HwndHook = Me.hWnd                 'intercept messages for my window
    Msghook1.Message(WM_QUERYENDSESSION) = True 'enable event for WM_QUERYENDSESSION
End Sub

Private Sub Msghook1_Message(ByVal msg As Long, ByVal wp As Long, ByVal lp As Long, result As Long)
    If msg = WM_QUERYENDSESSION Then
'        result = False
        MsgBox "Shutting Down"
'    Else
'        result = Msghook1.InvokeWindowProc(msg, wp, lp)
    End If
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Msghook1.Message(WM_QUERYENDSESSION) = False
    Msghook1.HwndHook = 0   'unhook any window
End Sub

How can I prevent shutdown so that no program should be closed?
Thanks


 
Um - you really didn't need to get involved with hooking anything; you are overengineering things. [tt]WM_QUERYENDSESSION[/tt] is presented as [tt]vbAppWindows[/tt] in any form's QueryUnload event - where you can easily set Cancel

Code:
[blue]Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbAppWindows Then Cancel = True
End Sub[/blue]

However ... all this achieves is stopping shutdown from the point it contacts your app. Plenty of other apps and processes may already have received shutdown notifications and responded to them, as you have noticed, BEFORE your app. And there's little you can do about that, as you have no control over a) the order in which Windows communicates with running applications or b) how quickly they will respond to the shutdown instruction. It also cannot block a forced shutdown request

 
Thanks, I had thought of this method earlier. Then I switched to sub-classing because we can intercept the messages of any window (not only our application). So if we can retrieve the handle of any system window that receives the message on the earliest my problem can be solved.

I tried specifying the desktop window handle. But it even doesn't block the shutdown. Any suggestions?

Thanks
 
>can intercept the messages of any window (not only our application)

No, we can't. WH_GETMESSAGE is not a global hook; it only works on windows owned by your own process
 
Thanks strongm, I was not aware of that. Anyway, I changed my mind. I have added a modal password form in the Form_QueryUnload event. I am not even checking UnloadMode. It is working fine, just some programs are closed but not mine!!!

Thank you everybody.
 
There are some other, arguably better solutions given at some of the other sites across the universe where this question was cross-posted.

Just a suggestion: this kind of cross-posting doesn't buy a lot of credibility in the community.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top