Solved

Message processing in console application

Posted on 1998-05-04
4
690 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
ID: 1307336
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
ID: 1307337
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
ID: 1307338
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
ID: 1307339
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 107
matchUp  challenge 6 70
Window placement 17 87
moto 4g phone enable finger print 4 28
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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