Thread: Receiving and Responding to Windows Messages

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Receiving and Responding to Windows Messages

    I've searched around but I'm still not entirely sure how it works. I understand the concept that, for example, Windows sends out the WM_QUERYENDSESSION message to all windows and that they then respond. But how?
    Code:
    #include <Windows.h>
    #include <stdio.h>
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    	if (iMsg == WM_QUERYENDSESSION) {
    		//respond
    	}
    }
    
    int main()
    {
    	while (1) {}
    	return 0;
    }
    Is that correct so far and the WindowProc function is called automatically when a message is recieved?
    Assuming that is correct, how do I respond to the message (either true or false)? Return from the WinowProc function?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) Get Thee over to MSDN and download the Windows SDK ... It's big, but it's well worth having. Full documentation of the windows API... including how to respond to WM_QUERRYENDSESSION ... (0 to block, 1 to allow)

    2) Take a visit to the Forger's Win API tutorial site ... Do the thing page by page, exercise by exercise... it's a pretty good start.

    FWIW... you almost always use... switch(iMsg) ... because there's so many of them... and you don't start a windows program with main... it's WindMain() ... The forger will guide you...

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    1) Get Thee over to MSDN and download the Windows SDK ... It's big, but it's well worth having. Full documentation of the windows API... including how to respond to WM_QUERRYENDSESSION ... (0 to block, 1 to allow)

    2) Take a visit to the Forger's Win API tutorial site ... Do the thing page by page, exercise by exercise... it's a pretty good start.

    FWIW... you almost always use... switch(iMsg) ... because there's so many of them... and you don't start a windows program with main... it's WindMain() ... The forger will guide you...
    I have downloaded the SDK, but can't find anything but example sources of seemingly irrelavant things.
    The 2nd link was useful but I didn't see anything on WM_QUERYENDSESSION and how to respond to it.
    Perhaps you could provide an example of a response to that message?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The thing is that you can't just stuff a windows callback function into a console program and have it work... It can be done but it's not easy.

    What you need to do is actually learn how to write a windows program... hense the Forger's tutorial...

    At bare minimum... you have to register at least one windows class, create a callback message tosser, create a message dispatcher and initiate a message handling loop... otherwise that WM_QUERYENDSESSION will never even get to your program...

    So, my best advice to you is to either A) settle in and learn windows programming or B) find another way to do whatever it is you're trying to do...

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    The thing is that you can't just stuff a windows callback function into a console program and have it work... It can be done but it's not easy.

    What you need to do is actually learn how to write a windows program... hense the Forger's tutorial...

    At bare minimum... you have to register at least one windows class, create a callback message tosser, create a message dispatcher and initiate a message handling loop... otherwise that WM_QUERYENDSESSION will never even get to your program...

    So, my best advice to you is to either A) settle in and learn windows programming or B) find another way to do whatever it is you're trying to do...
    When I'm not a big fan of Windows so I guess I would go with B. Although, I'm not entirely sure there's another way to delay the shutdown whilst my program finishes.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As far as I know the WM_ QUERYENDSESSION and WM_POWER broadcasts are the only way to postopone shutdown or standby on a windows platform.

    From the Windows perspective console programs are "quickies" to do small tasks and exit. "Real" application coding is to be done the "Windows way" and that's all done GUI style, like the Forger shows. It's not hard, once you get the hang of it. Windows controls actually make a lot of stuff easier to do than in console mode (getting inputs is a breeze) and you do have a lot more flexibility in the layout and appearance of your projects. But it does take a bit to get onto... the API is enormous, tens of throusands of functions, hundreds of messages... so there will be a learning curve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CTabCtrl not receiving messages
    By VirtualAce in forum Windows Programming
    Replies: 3
    Last Post: 07-23-2009, 09:37 AM
  2. clients registring and receiving messages
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 03-28-2008, 12:53 AM
  3. Sending/Receiving Messages
    By osal in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2005, 09:11 PM
  4. Receiving Messages from other Applications
    By 1943 in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2002, 12:00 AM
  5. Receiving messages from child of a child.
    By Sindol in forum Windows Programming
    Replies: 3
    Last Post: 01-26-2002, 07:58 AM