Solved

Using WM_QUERYENDSESSION with no defined windows

Posted on 2002-06-18
8
917 Views
Last Modified: 2007-12-19
Hi

I'm quite sure this is probably a stupid question. I know a bit of C++, but not Visual C++.

I have a project that runs without any created windows (it's a background process). I need for it to run continuously in the background, respond to messages (I have a working WndProc, I think) and terminate only when the user logs out.

I have a VC++ book in front of me, and it goes on about creating windows and registering window classes and stuff, but I don't want a window. Is this Window class just something you need to be able to get messages? Will it create a window?

My WndProc looks like this:
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch(uMsg)
  {
  case WM_QUERYENDSESSION:
    WriteLogout();
    break;
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    return(DefWindowProc(hwnd, uMsg, wParam, lParam));
  }
  return 0;
}
0
Comment
Question by:mollercw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7087710
What you have above will NOT create a window, but it also won't do what you want.

Only windows receive window messages like WM_QUERYENDSESSION.  But unless you call CreateWindow() (or one of its relatives) you'll not get any window at all.

I think, based on your description, that you DO want a window.  But your apps window need no be visible.  If you create it hidden (ShowWindow(SW_HIDE)) you can have a window, receive and process messages, but nobody will see the window.
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 7087771
you could always just try a simple message pump

MSG msg;
while (GetMessage(&msg, 0, 0, 0))
            DispatchMessage(&msg);

0
 

Author Comment

by:mollercw
ID: 7088297
ShaunWilde: Message pump? What does that do?

jhance: So I have to create a window class and register it? Do you have a simple example?
0
Independent Software Vendors: 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!

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7088396
the GetMessage receives messages sent to the thread and returns 0 if it receives a message with WM_QUIT - your app will receive that message if it has to close down due to a user logging out unless it is a service and then it will have its own shutdown mechanism
0
 

Author Comment

by:mollercw
ID: 7088465
Correct me if I'm wrong, but won't that just sit in an infinite loop and eat CPU waiting for a message?
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7088578
no - GetMessage waits until it receives a message unlike PeekMessage
0
 

Author Comment

by:mollercw
ID: 7088929
WHOOHOO it works =) I don't understand why, but it does. I decided to follow what the book says (seeing as you two gave me the two halves of what they said anyway). I created a WndProc, a Window class, the Window and the GetMessage loop. And it all works.

Not too shabby.

Thanks a lot for the help =)
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7088992
glad to help
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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