Solved

Message processing in console application

Posted on 1998-05-04
4
688 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 78
has77  challenge 9 85
mapBully challenge 6 134
Detect file exist or not 3 157
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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