flat assembler
Message board for the users of flat assembler.

Index > Windows > How do applications know to shutdown?

Author
Thread Post new topic Reply to topic
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 02 Sep 2008, 11:51
A thought i had the other day... When you shutdown windows, all programs close, but how do they know to close?

My first thought was that GetMessage would return WM_QUIT, in which case apps would close themselves.

Then not long after, i realized that method would in effect just terminate applications, not giving them the chance the close file handles and stuff in the normal way - on receive of WM_DESTROY, or whatever...

I am out of ideas, and don't exactly know what to test to find out - if i try to shutdown, everything will close and i wont see my results...

So does anyone know how applications know to close when windows is shutting down?


This is not an urgent thing or anything, i just couldn't think of a way it could be done Razz Probably something really simple which I didn't think of Razz
Post 02 Sep 2008, 11:51
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder 02 Sep 2008, 11:57
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:56; edited 1 time in total
Post 02 Sep 2008, 11:57
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 Sep 2008, 12:38
I think windows just sends WM_CLOSE to apps, and then terminates those which don't respond in time.
Post 02 Sep 2008, 12:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 02 Sep 2008, 12:53
Windows also sends the WM_ENDSESSION message. An application should process the message if it needs to do any special actions during shutdown.
Post 02 Sep 2008, 12:53
View user's profile Send private message Visit poster's website Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 14 Sep 2008, 01:01
ok... but what about processes with no message loop? would they just terminate after so many seconds?
Post 14 Sep 2008, 01:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 14 Sep 2008, 01:54
Apps without message loops and apps that are not responding to the WM_CLOSE message cannot be nicely closed. Windows will wait up to 30 seconds for the app to close, it will also display a window allowing the user to "End Now" without waiting. In either case (the user clicks "End Now" or 30 seconds have passed) Windows will forcibly terminate the process.
Post 14 Sep 2008, 01:54
View user's profile Send private message Visit poster's website Reply with quote
FrozenKnight



Joined: 24 Jun 2005
Posts: 128
FrozenKnight 15 Sep 2008, 01:14
The closing of a process quite simple. a process just needs to call the API TerminateProcess. it's the programs responsibility to determine how this happens i usually let windows do it for me by returning from my program to the system where the kernel calls it for me.

As for messages it is usually a good idea to direct WM_CLOSE code that will eventually call TerminateProcess, after you have done all of your cleanup of course.

If you want to see this in action try using my little KILL app, I've attached the source. to use this drag from it's the windows grey over any window window and it will die instantly no wait, no saving, no messages. Warning this will kill explorer and the desktop too. I did code an exception so it wont kill it's self.

Warning the new version of AntiVir seems to detect this as a virus which is why i didn't include the binary. if you are worried about it then just check the source.

Also note that some frozen windows will be treated as though they are the desktop, I personally consider this a flaw in windows as i have done some testing and found that this error is a result of what appears to be a windows bug.

I am not responsible for any crashes or data loss due to use of this program. this is a test model and not to be considered a tool. The Icon and Cursor are My art work and you may not use them with out my permission (If you want to use them just ask, I don't intend to be a hard ass about people using them i just want credit if you place them in your app)

Anyway in answer to your question, windows just loops through all the process handles and terminates them one at a time. of course it does give them a chance to close first usually a few seconds, you can change that value in the registry to let it wait longer or close faster)


Description:
Download
Filename: Kill_src.zip
Filesize: 7.6 KB
Downloaded: 256 Time(s)

Post 15 Sep 2008, 01:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 15 Sep 2008, 01:57
Just to note that calling ExitProcess is preferred to TerminateProcess. The Exit version allows the process to shut down cleanly whereas the Terminate version will abruptly stop the process uncleanly.
Post 15 Sep 2008, 01:57
View user's profile Send private message Visit poster's website Reply with quote
FrozenKnight



Joined: 24 Jun 2005
Posts: 128
FrozenKnight 16 Sep 2008, 00:31
from msdn
Quote:
If one of the terminated threads in the process holds a lock and the DLL detach code in one of the loaded DLLs attempts to acquire the same lock, then calling ExitProcess results in a deadlock. In contrast, if a process terminates by calling TerminateProcess, the DLLs that the process is attached to are not notified of the process termination. Therefore, if you do not know the state of all threads in your process, it is better to call TerminateProcess than ExitProcess. Note that returning from the main function of an application results in a call to ExitProcess.


This is why i use TerminateProcess it's a guaranteed kill, while exit process may be cleaner but it isn't as effective. the truely best result would be to call ExitProcess and if the process still exists one or two seconds later to call TerminateProcess.
Post 16 Sep 2008, 00:31
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.