Detecting Windows Shutdown

Posted on 2009-12-31
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.
Question by:SexyFast
    LVL 43

    Expert Comment

    These are the only ways I know of.

    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.


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

    Expert Comment

    WM_QUERYENDSESSION is only sent to apps which have a windows message loop. You need at least one (pseudo) window for that.
    LVL 7

    Expert Comment

    Why not make it a service? Then, SCM will call you by callback in case of a shutdown.

    Author Comment

    Any example code for having a message loop?
    LVL 7

    Accepted Solution

    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);

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

    Expert Comment

    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.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    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…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now