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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.