Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Message processing in console application

Posted on 1998-05-04
4
Medium Priority
?
697 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 140 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 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