Sign in to follow this  
Followers 0
this-is-me

$WM_QUERYENDSESSION and shutdown method

14 posts in this topic

Does anyone here know how to tell what type of shutdown the user is attempting (shutdown, logoff, restart) when $WM_QUERYENDSESSION is called? I know the message itself does not provide what type of shutdown, but I was wondering if any other function would tell me. My problem is making certain my program exits cleanly. I want to return 0 to the $WM_QUERYENDSESSION message, cleanup my script, and call the shutdown, logoff or restart function manually afterwards. I would prefer not to call an incorrect shutdown. If, for example, the user is logging off, I would not want to accidentally shutdown the computer.


Who else would I be?

Share this post


Link to post
Share on other sites



Did you carefully read MSDN? Two things I notice:

lParam If this parameter is zero, the system is shutting down or restarting (it is not possible to determine which event is occurring).

Also:

Applications should respect the user's intentions and return TRUE. By default, the DefWindowProc function returns TRUE for this message.

If shutting down would corrupt the system or media that is being burned, the application can return FALSE.

Share this post


Link to post
Share on other sites

If lparam is ENDSESSION_CLOSEAP, the application is using a file that must be replaced, the system is being serviced, or system resources are exhausted. If lparam is ENDSESSION_LOGOFF, the user is logging off. If lparam is 0, the system is shutting down or restarting and you'd have no way of knowing which is occuring, so you're out of luck in that instance. If you want to read up on this, check out this MSDN reference site.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

@Valik, As for comment #1, I am aware of the returns from the message, as is exemplified by my comment:

I know the message itself does not provide what type of shutdown,

However, I do see the fallacy in the example I gave in my last sentance. I was meaning that I would prefer not to restart if the user was shutting down, or shut down if the user is restarting. My original example was simply incorrect.

#2: The comments from MSDN are the very reason why my script will return false.

If shutting down would corrupt the system or media that is being burned, the application can return FALSE.

My script is running directly from a USB drive, and I am carefully cleaning up settings on the drive and the target system when the user shuts down to prevent corruption of the USB drive or the target system.

@PaulIA, I did see the MSDN article before posting, and agree that my example was incorrect.

@both, I was not wishing to know whether or not this message would tell whether the system was shutting down or rebooting, but whether any other API call could tell me that.

@Valik, I would like you to clarify one thing for me if you know the answer. When MSDN said the following

it is not possible to determine which event is occurring

In your estimation, did that mean that it is not possible to determine from this message, or if it meant it is not possible by any means whatsoever?

Who else would I be?

Share this post


Link to post
Share on other sites

@this-is-me: From your first post, I didn't know if you caught the bit on MSDN about the possible codes being passed. My post was purely for informative purposes, nothing else.

You can use SetConsoleCtrlHandler and set a handler that will give you a control event that tells you whether the system is logging off or shutting down. This happens fairly deep into the shutdown process, so you may want to check that out as a possible solution.

HTH


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

As far as I know, there's no way to detect what the system is doing in that regard.

Is it really necessary to stop the shutdown? Can't you create an WM_ENDSESSION handler which does your cleanup? Windows will let your program close and do cleanup if you handle WM_ENDSESSION and do your cleanup during the processing of that message.

Edit: PaulIA, how do you suggest registering the function in AutoIt when it's not possible to register AutoIt functions as callbacks to the Windows API? And even if it were possible, I don't see anything about being able to detect a restart versus a shutdown, anyway.

Edited by Valik

Share this post


Link to post
Share on other sites

I suggest doing a little research on google, as valik stated you should be able to care of bussiness when you handle WM_ENDSESSION.

I don't remember what registry key was mentioned, but if the default 30 seconds isn't long enough you can add a registry key to change the default time.

Also you might look into Enabling Shutdown Event Tracker

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\ShutdownReasonUI

Not sure if that one will help or not


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

You might want to try posting a question to: scripter@microsoft.com

and see if you get any useful info. I found that e-mail address at this link Technet - The Scripting Guy

Share this post


Link to post
Share on other sites

@PaulIA, I understand now the reason for your post. Let me reiterate the fact that I am indebted to you for your work on the Auto3Lib. I appreciate any information you are willing to convey to me.

@Valik, thanks for the info on WM_ENDSESSION. I was unaware that using this message will allow me to complete processing until I return a value. I will use that message instead.

@gafrost, I am certain that as of this time, 30 seconds is enough time to do the processing I require. However, in the future, do you think that setting the timeout value in the registry as my program is launching will allow windows to accept the new value at shutdown time?

@VeeDub, I was not aware that Microsoft provided free support for scripters like us on these types of issues. If emailing this address does indeed elicit a reasonable response, then it may be worth adding to our respective address books for any future issues of this kind.


Who else would I be?

Share this post


Link to post
Share on other sites

@gafrost, I am certain that as of this time, 30 seconds is enough time to do the processing I require. However, in the future, do you think that setting the timeout value in the registry as my program is launching will allow windows to accept the new value at shutdown time?

Don't think so


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Am I to assume that you believe the new setting only takes effect after windows is restarted at least once?


Who else would I be?

Share this post


Link to post
Share on other sites

Am I to assume that you believe the new setting only takes effect after windows is restarted at least once?

Correct


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

OK, thanks for your input. It is always appreciated.


Who else would I be?

Share this post


Link to post
Share on other sites

newbie here

anyone care to place a code example of how we can get the lParam to query for the ENDSESSION_LOGOFF ? :rolleyes:

thanks in advance

G

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0