• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 698
  • Last Modified:

Message processing in console application

How can I do message processing in a console application?

I have a console application in Windows NT 4.0 that must receive data coming from a third part DLL that uses PostMessage() to send the data. How can I do that?
  • 2
1 Solution
You need to set up a message loop.. eg.

MSG msg = { 0, 0, 0, 0 };
while ( GetMessage(&msg, 0, 0, 0) )
  // test for the message you are interested in
  // otherwise
  DispatchMessage( &msg );
  // you may want to jump out of this loop at some stage

you may want to put this sort of handler in thread so that your program doesn't sit around doing nothing while waiting for a message (unless that is what you want it to do).

marakrAuthor Commented:
Thank you for your simple and fast answer.

I have tested your answer with the following code:

#include <stdio.h>
#include <windows.h>
#include <process.h>

HWND hwnd;

void main( void )
   char szTitle[80];

   // Fetch current window title
   GetConsoleTitle(szTitle, 80);

   // Get window handle
   hwnd=FindWindow(NULL, szTitle);

   // Set a 1 second timer to test the message loop
   SetTimer( hwnd, 1, 1000, NULL );
   // Message loop
   MSG Msg;
   while( GetMessage( &Msg, hwnd, 0, 0 ) )


The 'Message!' printout never appears, even past 1 second. I have tried a PostMessage( hwnd,  WM_USER, 0, 0) too, but still no message appears. Keep in mind that this is a console application.

By the way: The help for GetMessage() says "(...)If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread via PostThreadMessage. GetMessage does not retrieve messages for windows that belong to other threads nor for threads other than the calling thread.(...)". Even so I could create a thread with the message loop? Which would be the windows handler to send messages for?

Many thanks again for your help.
don't use hwnd in the GetMessage call.  Nor should you do a FindWindow etc.  You don't need to know where to get the message from.  It will get to you .. the problem will be with the sender, as it will need the handle of a window that belongs to your app.  I'll look into it a bit more.

You can't send messages to the console window (or any of the related windows). It's not your window, it has a special relationship with the window manager, and it already has its own message pump. The console window actually runs on the server-side of the system architecture: it's internal and private.

If you wrote your code more carefully, you would've noticed that SetTimer() returns FALSE indicating that it fails: the console window is owned by the system, and the system doesn't give priveleges to send messages to the (On WinNT, you'll see that GetLastError is ERROR_ACCESS_DENIED for the SetTimer() call.)

Note that you can find bogus a KB article (Q102482) that recommends against using SetTimer() in console applications; the article is hogwash: you can use a timer in a console app--but you just need to know what you're doing.

You can get your code above to work by passing NULL for the window handle _and_ passing NULL for time callback proc. This results in WM_TIMER messages being sent to the thread, not a particular window.  Then, call GetMessage() passing NULL for the window handle parameter so you receive thread messages.

If you want to provide a handle for some other application to send messages to with PostMessage(), you need to create a window. You can create a window in an app marked /SUBSYTEM:CONSOLE; it's no big deal.

.B ekiM

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now