|
-
Nov 9th, 2012, 09:45 AM
#1
Thread Starter
New Member
[RESOLVED] Prevent windows shutdown
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 http://vb.mvps.org/tools/MsgHook/ 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
-
Nov 9th, 2012, 10:15 AM
#2
Re: Prevent windows shutdown
You will need to test and this is at the form level but I think you can trap it this way:
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbFormControlMenu
MsgBox "The user chose the Close command (the ""X"") from the Control menu on the form."
Case vbFormCode
MsgBox "The Unload statement is invoked from code."
Case vbAppWindows
MsgBox "The current Microsoft Windows operating environment session is ending."
Case vbAppTaskManager
MsgBox "The Microsoft Windows Task Manager is closing the application."
Case vbFormMDIForm
MsgBox "An MDI child form is closing because the MDI form is closing."
Case vbFormOwner
MsgBox "A form is closing because its owner is closing."
End Select
End sub
focus on:
Case vbAppWindows
MsgBox "The current Microsoft Windows operating environment session is ending."
Case vbAppTaskManager
MsgBox "The Microsoft Windows Task Manager is closing the application."
-
Nov 9th, 2012, 12:41 PM
#3
Re: Prevent windows shutdown
" 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." -- that's not surprising... they're probably getting closed down before your app is requested to close. your app may be stopping the shutdown process, but if other apps/services have already shut down... you can't really do a whole lot about that.
-tg
-
Nov 9th, 2012, 01:41 PM
#4
Re: Prevent windows shutdown
 Originally Posted by vbdev100
1. ... How can I return a value while using MsgHook.
You were correct in using the result [out] parameter.
 Originally Posted by vbdev100
2. I used following code without returning any value. It works, ...
That's because functions return False by default. However, the Else clause in your code shouldn't have been commented out. It prevents other messages you may be watching for from reaching the previous window procedure.
 Originally Posted by vbdev100
How can I prevent shutdown so that no program should be closed?
MSDN says:
If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.
When an application returns TRUE for this message, it receives the WM_ENDSESSION message, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Each application should return TRUE or FALSE immediately upon receiving this message, and defer any cleanup operations until it receives the WM_ENDSESSION message.
That means the OS sequentially sends the WM_QUERYENDSESSION to each application. If an application closes before another one postpones the shutdown, then there's not much that can be done about that. However, if your app starts early enough (and will be running throughout), then it'll be the first one that the OS sends the WM_QUERYENDSESSION message to. If your app returns FALSE, then you effectively prevented other apps from receiving WM_QUERYENDSESSION and thus closing.
If you're using VB5/6, you actually don't need the MsgHook component because the AddressOf operator available in those versions enable native subclassing. Also, VB already has the capability you're looking for, as TysonLPrice showed you. Here's code equivalent to yours:
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then Cancel = True
End Sub
For Vista and later OSes, the ShutdownBlockReasonCreate function et al. is the proper way of telling the OS not to shutdown. BTW, users who are prevented from shutting down their computers are most likely going to turn it off at the switch. Happened to me once with a DVD-burner app.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 9th, 2012, 01:49 PM
#5
Re: Prevent windows shutdown
 Originally Posted by vbdev100
Now I am trying to prevent windows from shutting down, restart or logging off while my application is running.
Why ?
-
Nov 10th, 2012, 02:07 AM
#6
Thread Starter
New Member
Re: Prevent windows shutdown
 Originally Posted by Doogle
Why ?
Because, my application should monitor some machines continuously & it should not be closed by any means.
-
Nov 10th, 2012, 02:12 AM
#7
Re: Prevent windows shutdown
Then (IMHO) it should be running as a 'non-stoppable Service'. You shouldn't be preventing a machine from shutting down and your Service should respect a machine Shutdown request.
-
Nov 10th, 2012, 02:27 AM
#8
Thread Starter
New Member
Re: Prevent windows shutdown
 Originally Posted by TysonLPrice
You will need to test and this is at the form level but I think you can trap it this way:
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Select Case UnloadMode
Case vbFormControlMenu
MsgBox "The user chose the Close command (the ""X"") from the Control menu on the form."
Case vbFormCode
MsgBox "The Unload statement is invoked from code."
Case vbAppWindows
MsgBox "The current Microsoft Windows operating environment session is ending."
Case vbAppTaskManager
MsgBox "The Microsoft Windows Task Manager is closing the application."
Case vbFormMDIForm
MsgBox "An MDI child form is closing because the MDI form is closing."
Case vbFormOwner
MsgBox "A form is closing because its owner is closing."
End Select
End sub
focus on:
Case vbAppWindows
MsgBox "The current Microsoft Windows operating environment session is ending."
Case vbAppTaskManager
MsgBox "The Microsoft Windows Task Manager is closing the application."
Thanks, vbAppWindows works perfectly. I added 'Cancel = True' & shutdown was prevented. But some programs were ended earlier.
Your solution also clicked me an idea of preventing to end my application from task manager. So I tried it but I get the program is not responding message. The shutdown can continue either automatically after some period or by pressing the end button. Anyway, this was not my requirement.
If windows sends the WM_QUERYENDSESSION message in some particular sequence/priority (may be some system windows like desktop gets it earlier), I can trap the message from there???
-
Nov 10th, 2012, 02:41 AM
#9
Thread Starter
New Member
Re: Prevent windows shutdown
Bonnie West, thanks!
I commented out the Else clause in my code, because only the WM_QUERYENDSESSION message is enabled to trap in MsgHook. So even 'If' clause is also not necessary (I think) as I have enabled only one message.
You were correct in using the result [out] parameter.
I am still not able to understand the return method. Is result = False alright?
Thanks.
-
Nov 10th, 2012, 03:29 AM
#10
Re: Prevent windows shutdown
 Originally Posted by vbdev100
Is result = False alright?
Yes.
Notice that the result parameter is declared implicitly As ByRef. That means the original variable's memory address was passed to that function so that you can directly manipulate its value.
Please refer to the MSDN doc Passing Arguments to Procedures for a more complete discussion.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 10th, 2012, 03:59 AM
#11
Re: Prevent windows shutdown
 Originally Posted by vbdev100
If windows sends the WM_QUERYENDSESSION message in some particular sequence/priority (may be some system windows like desktop gets it earlier), I can trap the message from there???
According to my tests, the system sends that message in the order that programs were started. So, if you could ensure that your app starts before everything else, there's a good chance it will be able to block that message from reaching the others.
My tests seem to indicate that the explorer.exe process waits for all the processes' response before it decides what to do. If one app returns FALSE after the others exited, the explorer process remains alive.
I'm not sure if the WM_QUERYENDSESSION message can be trapped with Hooking...
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 10th, 2012, 04:35 AM
#12
Re: Prevent windows shutdown
 Originally Posted by Doogle
Then (IMHO) it should be running as a 'non-stoppable Service'. You shouldn't be preventing a machine from shutting down and your Service should respect a machine Shutdown request.
I agree with Doogle.
I'm curious how your program is going to prevent a user from just pulling the power-cord.....
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 10th, 2012, 07:01 AM
#13
Thread Starter
New Member
Re: Prevent windows shutdown
Thanks Bonnie West,
Now I understood the result parameter correctly.
For now, I am quitting the idea of hooking any window. As I don't know beforehand which window will be first to get the message. So either using the WM_QUERYENDSESSION message or deny in Form_QueryUnload event will be more or less the same thing. And yes, the WM_QUERYENDSESSION message can be trapped because I got the 'shutting down' message which was in Msghook1_Message event.
I used the following code to popup password screen to allow exit/shutdown. (Though some programs are closed, but mine NOT). I did not check for UnloadMode.
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If PwdExit Then
PwdType = Pwd.ProgExit
frmPassword.Show vbModal, Me
If Not FrmEnding Then
Cancel = True
Exit Sub
End If
End If
Screen.MousePointer = vbHourglass
tmrTime.Enabled = False
tmrTimeOut.Enabled = False
CloseAllRecords
Unload frmSock
RemoveIcon Me
' FrmEnding = True
If Not qryShift Is Nothing Then qryShift.Close
Set qryShift = Nothing
Call CloseDB
.
.
.
.
-
Nov 10th, 2012, 04:06 PM
#14
Re: Prevent windows shutdown
So, if you could ensure that your app starts before everything else,
do you know some way to do that?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 10th, 2012, 05:02 PM
#15
Hyperactive Member
Re: Prevent windows shutdown
this in a module
Code:
Option Explicit
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll" 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 WM_QUERYENDSESSION As Long = &H11
Public Const GWL_WNDPROC As Long = -4
Public oldProc As Long
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If iMsg = WM_QUERYENDSESSION Then
WindowProc = 0
Else
WindowProc = CallWindowProc(oldProc, hwnd, iMsg, wParam, lParam)
End If
End Function
with this in form of project should prevent windows from shutting down
Code:
Option Explicit
Private Sub Form_Load()
oldProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, oldProc
End Sub
-
Nov 12th, 2012, 02:24 AM
#16
Thread Starter
New Member
Re: Prevent windows shutdown
Thanks, this small & clean code is really self-explanatory.
-
Nov 12th, 2012, 02:41 AM
#17
Re: Prevent windows shutdown
The WindowProc function can be simplified a bit:
 Originally Posted by Rattled_Cage
Code:
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If iMsg <> WM_QUERYENDSESSION Then WindowProc = CallWindowProc(oldProc, hwnd, iMsg, wParam, lParam)
End Function
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 12th, 2012, 02:48 AM
#18
Re: Prevent windows shutdown
 Originally Posted by westconn1
do you know some way to do that?
The Sysinternals' Autoruns for Windows should be quite helpful.
EDIT
Here are a couple more potentially useful links:
Last edited by Bonnie West; Aug 15th, 2016 at 06:44 PM.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Nov 13th, 2012, 12:52 AM
#19
Re: Prevent windows shutdown
hmm .... I certianly would not want any program on my computer that prevented me from doing a normal shutdown or restart nor would I create such a thing as it would be very intrusive.
Basically such a program would prevent you from being able to install some software, updates or just restart when the system was unstable and would force the user to power off the computer or hit the reset button neither of which is a good idea.
Should sucha thing exist on one of my systems the first thing I would do is shut it down then boot to a different OS and delete the program and all hooks to it just like I would with any other virus or malicous software that is not auto removed by my virus scanner.
-
Nov 13th, 2012, 04:21 AM
#20
Re: Prevent windows shutdown
I certianly would not want any program on my computer that prevented me from doing a normal shutdown or restart
i would be very keen to have something like this, to prevent automatic reboots by windows updates or other programs that think it is their god given right to control my computer when ever they like regardless of what data i may loose
i would of course prefer it written by me, with option to continue shutdown if i was the one initiating the shutdown
i will look at the link posted above to see if i can make my program the first to be loaded, so that no data will be lost from other programs
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 16th, 2013, 05:45 AM
#21
Lively Member
Re: Prevent windows shutdown
Hello,
This code does not work for Windows Vista, Windows 7 and Windows 8 ... Is there another valid code for these operating systems?
Thank you very much!
 Originally Posted by Rattled_Cage
this in a module
Code:
Option Explicit
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll" 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 WM_QUERYENDSESSION As Long = &H11
Public Const GWL_WNDPROC As Long = -4
Public oldProc As Long
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If iMsg = WM_QUERYENDSESSION Then
WindowProc = 0
Else
WindowProc = CallWindowProc(oldProc, hwnd, iMsg, wParam, lParam)
End If
End Function
with this in form of project should prevent windows from shutting down
Code:
Option Explicit
Private Sub Form_Load()
oldProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, oldProc
End Sub
-
Jun 16th, 2013, 09:14 AM
#22
Re: Prevent windows shutdown
 Originally Posted by Korku
This code does not work for Windows Vista, Windows 7 and Windows 8 ... Is there another valid code for these operating systems?
In case you've overlooked post # 4 above, see the last paragraph.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 16th, 2013, 03:45 PM
#23
Re: Prevent windows shutdown
 Originally Posted by westconn1
i would be very keen to have something like this, to prevent automatic reboots by windows updates or other programs that think it is their god given right to control my computer when ever they like regardless of what data i may loose
i would of course prefer it written by me, with option to continue shutdown if i was the one initiating the shutdown
i will look at the link posted above to see if i can make my program the first to be loaded, so that no data will be lost from other programs
Isn't there settings on windows to do this already, to ask a user if he wants to install updates, or automatically install updates.
This would prevent windows from doing automatic reboots, but maybe not other programs.
To be honest I never had a program restart my computer for me, it always asked me if I wanted to reboot later or reboot now.
I guess it could happen and if your working on a project (VB,EXCEL,PHOTO SHOP,AUTOCAD) and it shuts down windows and you forgot to save for an hour or more then it would ruin the whole project!
In this case a program to prevent from rebooting would actually be fantastic!
-
Jun 17th, 2013, 02:44 AM
#24
Lively Member
Re: Prevent windows shutdown
 Originally Posted by Bonnie West
In case you've overlooked post # 4 above, see the last paragraph.
Ok, do you have a code example of how to run the function ShutdownBlockReasonCreate? Thank you!
-
Jun 17th, 2013, 03:23 PM
#25
Re: Prevent windows shutdown
 Originally Posted by Korku
Ok, do you have a code example of how to run the function ShutdownBlockReasonCreate? Thank you!
Code:
Option Explicit
'Reason string should be <= MAX_STR_BLOCKREASON, else it is truncated
Private Const SHUTDOWN_BLOCK_REASON As String = "Unable to shutdown for some reason."
Private Const MAX_STR_BLOCKREASON As Long = 256
Private Declare Function ShutdownBlockReasonCreate Lib "user32.dll" (ByVal hWnd As Long, ByVal pwszReason As Long) As Long
Private Declare Function ShutdownBlockReasonDestroy Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Sub Form_Load()
Dim RetVal As Long
RetVal = ShutdownBlockReasonCreate(hWnd, StrPtr(SHUTDOWN_BLOCK_REASON))
Debug.Assert RetVal 'If code stops here, ShutdownBlockReasonCreate failed
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then Cancel = True 'Still need to do this
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim RetVal As Long
RetVal = ShutdownBlockReasonDestroy(hWnd)
Debug.Assert RetVal 'If code stops here, ShutdownBlockReasonDestroy failed
End Sub
Application Shutdown Changes in Windows Vista is highly recommended reading!
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Jun 17th, 2013, 04:29 PM
#26
Re: [RESOLVED] Prevent windows shutdown
Isn't there settings on windows to do this already, to ask a user if he wants to install updates, or automatically install updates.
This would prevent windows from doing automatic reboots, but maybe not other programs.
if the user does not click later, it will wait a short while then reboot, when running unattended this can be a problem, also i have in the past clicked the default (restart now) when the dialog pops up, as focus had changed from what i was previously doing, some registry changes did help, but still have issues on W7 when automatic updates are on
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 18th, 2013, 02:35 AM
#27
Lively Member
Re: Prevent windows shutdown
Works fine! Thank you very much!
But it's not what I want. What I want is that when you try to shut down the computer with the application open, display a message something like "You must close the application before turning off the computer."
Also this sample code closes the programs that are in the background.
I'm trying to make a remote connection application for a tablet with Windows 8, I wanted to avoid placing in sleep mode or turned off when the application is open.
 Originally Posted by Bonnie West
Code:
Option Explicit
'Reason string should be <= MAX_STR_BLOCKREASON, else it is truncated
Private Const SHUTDOWN_BLOCK_REASON As String = "Unable to shutdown for some reason."
Private Const MAX_STR_BLOCKREASON As Long = 256
Private Declare Function ShutdownBlockReasonCreate Lib "user32.dll" (ByVal hWnd As Long, ByVal pwszReason As Long) As Long
Private Declare Function ShutdownBlockReasonDestroy Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Sub Form_Load()
Dim RetVal As Long
RetVal = ShutdownBlockReasonCreate(hWnd, StrPtr(SHUTDOWN_BLOCK_REASON))
Debug.Assert RetVal 'If code stops here, ShutdownBlockReasonCreate failed
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbAppWindows Then Cancel = True 'Still need to do this
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim RetVal As Long
RetVal = ShutdownBlockReasonDestroy(hWnd)
Debug.Assert RetVal 'If code stops here, ShutdownBlockReasonDestroy failed
End Sub
Application Shutdown Changes in Windows Vista is highly recommended reading! 
-
Jun 18th, 2013, 02:28 PM
#28
Re: Prevent windows shutdown
 Originally Posted by Korku
But it's not what I want. What I want is that ...
You should've made that clear right from the start. 
 Originally Posted by Korku
... when you try to shut down the computer with the application open, display a message something like "You must close the application before turning off the computer."
ShutdownBlockReasonCreate already does that. Isn't that what you want? The SHUTDOWN_BLOCK_REASON constant in the example code in post #25 above can be modified as desired.
 Originally Posted by Korku
Also this sample code closes the programs that are in the background.
Well, it could be because of the reason mentioned in posts #3 & #4. It might also be because those programs have no visible top-level window. As discussed in the Application Shutdown Changes in Windows Vista:
 Originally Posted by MSDN
Windows Vista will also not allow console applications or applications that have no visible top-level windows to block shutdown. In most cases, such applications are less important to users at shutdown than applications that do have visible top-level windows. If an application without a visible top-level window blocks shutdown by vetoing WM_QUERYENDSESSION, or takes over 5 seconds to respond to WM_QUERYENDSESSION or WM_ENDSESSION, Windows will automatically terminate it.
 Originally Posted by Korku
I'm trying to make a remote connection application for a tablet with Windows 8, I wanted to avoid placing in sleep mode or turned off when the application is open.
As pointed out several times already, you shouldn't prevent the user from shutting down the computer. Even Microsoft agrees:
 Originally Posted by Application Shutdown Changes in Windows Vista
Applications should not block shutdown
If you take only one thing away from reading this topic, it should be this one. You will be presenting the best experience to your users if your application does not block shutdown. When users initiate shutdown, in the vast majority of cases, they have a strong desire to see shutdown succeed; they may be in a hurry to leave the office for the weekend, for example. Applications should respect this desire by not blocking shutdown if at all possible.
In many cases, applications that block shutdown can be redesigned so that they no longer need to do so. For example, applications with unsaved data at shutdown can be designed to automatically save their data and restore it when the user next starts the application, instead of blocking shutdown to ask the user what to do. Microsoft Office 2007 will do this in certain scenarios.
In other cases, there is usually a sensible default action that applications should automatically perform, instead of blocking shutdown to ask the user what to do. In many cases, this is true even if the default action could be slightly destructive. Read more...
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
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
|