I have a program that reacts to WM_QUERYENDSESSION to perform some cleanup running as a scheduled task. Logging indicates that the cleanup code is not executed. Do applications running as a scheduled task receive WM_QUERYENDSESSION messages or is there any other way to detect Window shutdown?
The messages are sent to top level windows. If your process has one, it will be sent the message. If you don't have one, then you can create one for that purpose. If your application is a console application then SetConsoleCtrlHandler is the recommended way to receive such notification.
It transpires, from the comment thread, that your process is running under the SYSTEM account. According to the documentation, this means that it will not be shutdown by the system.
Applications running in the system security context do not get shut down by the operating system. They get notified of shutdown or logoff through the callback function installable via SetConsoleCtrlHandler.
Related
I have a program that shuts down another application when certain conditions are met. Now most of the time everything works out fine, but sometimes the app is writing to a file and leaves it in a half finished state and has no way of recovering it on restart. I thought that one could send soft close signals and escalate after certain timeouts to more aggressive close signals, going trough a list like this:
1. WM_CLOSE
2. WM_QUIT
3. WM_DESTROY
4. TerminateProcess().
Now I know that the program has no code to handle any signal it receives. Is there a possibility that certain FileHandler under Windows react gracefully on such soft signals or is there no use to sending those, if the app does not handle them explicitly?
This article says:
NOTE: A console application's response to WM_CLOSE depends on whether or not it has installed a control handler.
Does this mean if no control handler is installed sending 1-4 is just as good as sending 4 directly?
In Windows, When a new process is started, what windows message is send ? http://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm
I'm not exactly sure if you are asking about a message an app gets when a child process starts. Or if there is a message sent when another process on the same computer starts. Or if you are asking about a default message every process gets.
But the answer is the same: None.
There are no window messages sent in regards to starting a new process.
What would you do if there was?
I have a simple program to process messages from a queue.
My intention is to process all available messages in queue and still listen to queue for incoming messages.
I have written the processing part inside a infinite loop as i want it to listen to queue always and process messages.
Once after processing all messages again it tries to get a message(as it is inside a infinite loop) from the queue and there
is no messages it throws MQRC 2033 NO_MSG_AVAILABLE exception(infact it is correct) and my program exits.
Can someone give an idea to continously listen to this queue and avoid this exception.
When you execute the MQGET API call, there is an option to have the program wait for messages. You can specify a wait time (in milliseconds) or specify to wait forever. Just make sure that if you have the app wait for more than a few seconds, also specify 'Fail if Quiescing'. This allows the queue manager to be stopped cleanly. Without 'Fail if Quiescing' the administrator will need to issue a preemptive shutdown which can cause problems.
There is a section specifically for this question in the Programmer's Guide in the Waiting for Messages chapter. Depending on the language you are writing in ,the actual value to specify is in the Programmer's Reference, the Using Java manual or the Using .Net manual. Each of these will be visible in the navigation panel when you click the link above.
I have small app with a Window that is listening for the WM_QUERYENDSESSION message and returning FALSE - which should tell Windows that I don't want it to shutdown (see this MSDN link).
What I have found is that even though I am asking Windows to not shutdown, and Windows is itself not shutting down, it is still sending the WM_ENDSESSION message and closing other applications that are running alongside my own.
Does anyone know why this is happening and what can be done so that my application also prevents other applications from shutting down?
I have found that the order in which I start my application and other applications on the system affects which ones are shutdown and which are not.
A bit more research uncovered a system call to SetProcessShutdownParameters(). Using this call I can place my application at the front of the list of processes to be asked about shutting down Windows and so prevent the other applications from ever getting the WM_ENDSESSION message.
So to summarize:
When a shutdown event occurs (shutdown, restart or log off), Windows sends out a WM_QUERYENDSESSION message to each application in turn.
If an application doesn't object to the shutdown (they return TRUE), they are then sent a WM_ENDSESSION message.
As soon as one application returns FALSE to the WM_QUERYENDSESSION the shutdown is aborted, and no further messages are sent out.
Please look into this msdn article: http://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx
Basically, it really depends on what each application do with the WM_QUERYENDSESSION and the WM_ENDSESSION messages. As stated in the above article, WM_ENDSESSION messages are sent no matter what with the results of the WM_QUERYENDSESSION messages. Many applications decide to shutdown no matter the result.
There is also many application that start their shutdown process right after receiving the WM_QUERYENDSESSION to give them more time to shutdown before the dreaded "this application is not responding..." dialog
Hope this clarifies things a bit
Under POSIX OS there is signal API that allows to send a signal to process to shut it down
with kill and you can catch it with sigaction and do what you need;
However, Win32 is not POSIX system, so:
How can I handle shutdown events that may come, for example from "End Process" in "Task manager"?
What is the standard API for sending shutdown signal to Win32 application?
I'm not talking about GUI, I'm talking about TCP/IP server that should be nicely shutdown. that does not run like windows service.
MSDNs Unix Code Migration Guide has a chapter about Win32 code conversion and signal handling.
Although Microsoft has decided to archive this brilliant guide, it is very useful.
Three methods are described:
Native signals
Event objects
Messages
You get a WM_QUIT message on your first created thread.
When you don't handle that, your process is forcibly shutdown.
So just implement a message queue in your first thread, which looks for the WM_QUIT message
May be Windows Power Management from MSDN would be helpful. But it deals with system events rather than per process.
For a process, you would be able to detect termination with WM_CLOSE. You would need to handle windows messages. If it's a console application you would need to install a control handler; take a look at SetConsoleCtrlHandler on MSDN