Link to home
Start Free TrialLog in
Avatar of AndrewR
AndrewR

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
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...
Oops, we are talking about an NT service. jkr is right.
Avatar of AndrewR
AndrewR

ASKER

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> :-)
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???
>>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);
}

;-)
Avatar of AndrewR

ASKER

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.
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?
Avatar of AndrewR

ASKER

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...