Solved

Using WM_QUERYENDSESSION with no defined windows

Posted on 2002-06-18
8
891 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 218
getting a fire fox page (view page source ) in c++ 6 42
Compile GLUT with Visual Studio 2015 1 108
Path to  STL Map header file 1 41
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 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 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.

914 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

14 Experts available now in Live!

Get 1:1 Help Now