How To Tell That Windows Is Shutting Down

I need to be able to tell that windows is in the process of shutting down, so that I can avoid trying to link to some dll's and getting an error.

I have a service which receives the WM_QUERYENDSESSION message, but another application pops up a messagebox asking are you sure etc...
Meanwhile the service spawns another process. This one doesn't get the WM_QUERYENDSESSION and has problem linking to some dll's. I need to be able to know that windows is shutting down... Any ideas?
AndrewRAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
chensuConnect With a Mentor Commented:
The WM_ENDSESSION message is sent to an application after the system processes the results of the WM_QUERYENDSESSION message. The WM_ENDSESSION message informs the application whether the session is ending.
0
 
jkrCommented:
Hmm, a service usually receives a 'SERVICE_CONTROL_SHUTDOWN' reuqest when this is happening - if you handle this code in your service's control handler, everything should be fine...
0
 
chensuCommented:
Oops, we are talking about an NT service. jkr is right.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
AndrewRAuthor Commented:
so there is no way to tell at the beginning of a program that windows is in the process of shutting down?
There is an ERROR_SHUTDOWN_IN_PROGRESS error, but I haven't found a function that will generate this for me...

A nice little function like
BOOL WindowsShutdownInProgress()
would be great... ,<sigh> :-)
0
 
chensuCommented:
The functions generating the ERROR_SHUTDOWN_IN_PROGRESS error are the following.

InitiateSystemShutdownEx
QueryServiceStatusEx
ControlService
EnumServicesStatusEx

But three of them require Windows 2000. Maybe you can use ControlService???
0
 
jkrCommented:
>>Maybe you can use ControlService???

AndrewR has to use it, or the service could never be shut down...

>>A nice little function like
>>BOOL WindowsShutdownInProgress()
>>would be great... ,<sigh> :-)

What about

BOOL WindowsShutdownInProgress()
{
BOOL bShuttingDown = FALSE;
if ( !InitiateSystemShutdown ( NULL, "message",  5, FALSE, FALSE))
{
  if ( ERROR_SHUTDOWN_IN_PROGRESS == GetLastError())
    {
      bShuttingDown == TRUE;
    }
}

 AbortSystemShutdown( NULL);

 return ( bShuttingDown);
}

;-)
0
 
AndrewRAuthor Commented:
If the WM_QUERYENDSESSION messages have all been sent by Windows, can a new process be create? It seems like it can, but I would have thought that Windows would prevent this.
0
 
jkrCommented:
Well, this usuall results in sth. like 'user32.dll couldn't be initialized...', as you have experienced... BTW: Did you try my suggestion to detect whether a shutdown is in progress?
0
 
AndrewRAuthor Commented:
I did try it but I still have the problem. Looks like it could be a timing issue. Between the time the service starts the child process and the child process initialized the dll, the windows shutdown state changes...
0
All Courses

From novice to tech pro — start learning today.