Solved

Message processing in console application

Posted on 1998-05-04
4
686 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:marakr
  • 2
4 Comments
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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).

0
 

Author Comment

by:marakr
Comment Utility
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 ) )
      {
      printf("Message!\r\n");
      DispatchMessage(&Msg);
      }

   }

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.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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.

0
 
LVL 11

Accepted Solution

by:
mikeblas earned 70 total points
Comment Utility
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

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 98
isEverywhere  challenge 19 61
changeXy challenge 13 56
wordappend challenge 8 82
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

6 Experts available now in Live!

Get 1:1 Help Now