?
Solved

Detecting Windows Shutdown

Posted on 2009-12-31
6
Medium Priority
?
869 Views
Last Modified: 2013-12-14
I'm writting a windows app, that has no UI, no windows, is not a service, and is not a console application.
How can I detect a windows shutdown so my application can perform a clean exit?
Example code is useful too.
Thanks in advance.
0
Comment
Question by:SexyFast
  • 4
6 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 26157399
These are the only ways I know of.


WM_QUERYENDSESSION
 
The WM_QUERYENDSESSION message is sent when the user chooses to end  the session or when an application calls the ExitWindows function. If any application  returns zero, the session is not ended. The system stops sending  WM_QUERYENDSESSION messages as soon as one application returns zero.


and

WM_ENDSESSION
 
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
 
LVL 7

Expert Comment

by:js-profi
ID: 26157483
WM_QUERYENDSESSION is only sent to apps which have a windows message loop. You need at least one (pseudo) window for that.
0
 
LVL 7

Expert Comment

by:js-profi
ID: 26157489
Why not make it a service? Then, SCM will call you by callback in case of a shutdown.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:SexyFast
ID: 26186847
Any example code for having a message loop?
0
 
LVL 7

Accepted Solution

by:
js-profi earned 1000 total points
ID: 26188020
first you need a window (pseudo) created by CreateWindow where you pass a function  pointer to a windows proc supposed to handle message. if window handle hwnd was valid you could run a message loop like

    MSG msg;
    while ( ::PeekMessage( &msg, hwnd, 0, 0, PM_NOREMOVE ) )
    {
          GetMessage(&msg, hwnd, 0, 0);
          TranslateMsg(&msg;
          DispatchMessage(&msg);
    }

the last will call the windows proc you provided. the thread which runs that loop will be blocked after that until WM_QUIT message. you need to add a handling that breaks the loop.
0
 
LVL 7

Expert Comment

by:js-profi
ID: 26188029
-----
the thread which runs that loop will be blocked after that
-----

ignore that. PeekMessage only peeks as long as there are messages. you need a handling after loop so that the message loop was called again until you want to stop.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question