Solved

Using WM_QUERYENDSESSION with no defined windows

Posted on 2002-06-18
8
897 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 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