Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using WM_QUERYENDSESSION with no defined windows

Posted on 2002-06-18
8
Medium Priority
?
940 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
  • 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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 a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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…
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…
Suggested Courses

879 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