Solved

Using WM_QUERYENDSESSION with no defined windows

Posted on 2002-06-18
8
885 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now