Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetMessage() in MFC console app

Posted on 2010-08-23
11
Medium Priority
?
1,800 Views
Last Modified: 2013-11-20
Hi all,
I have an MFC console application, with main declared as follows:
int _tmain(int argc, _TCHAR* argv[])

Can I receive windows standard messages, key up/down etc.?
If I call GetMessage() it never returns (unless I send a message myself with PostThreadMessage), nothing even gets printed on the screen when I type - the thread is blocked, just waiting.

Any help is appreciated.
0
Comment
Question by:darrgyas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 33501287
Well, what are you trying to achieve?
0
 

Author Comment

by:darrgyas
ID: 33501444
I need to shut the application down from a different thread, and I also need to have an option for the user to type, let's say, 'quit', push enter and exit that way.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33502237
GetMessage does block if there are not messages in the thread's msg queue. You could call PeekMessage and poll the msg queue. When you say a thread is going to send the message, it the thread owned by the same process?
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33502693
>> I need to shut the application down from a different thread, and I also need to have an option for the user to type, let's say, 'quit', push enter and exit that way

So you don't need GetMessage in this console application. Make a code that will wait for the user input and parse it. If it will be the command you want to handle, you will send a message to another process, for example, WM_CLOSE. You can SendMessage, emulate keyboard, ... from this console application.
0
 

Author Comment

by:darrgyas
ID: 33502746
>>>> When you say a thread is going to send the message, it the thread owned by the same process?
Yes

>>>> o you don't need GetMessage in this console application. Make a code that will wait for the user input and parse it
getc() blocks, the only option that it leaves me is send keys from another thread - that is not reliable
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33502768
So, maybe, the console application was not a good choice?
Simple MFC dialog with few button, an editbox as a console if needed,... - it's not a lot of work.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33502799
You could also just create an MFC dialog (with the window starting up hidden) like pgnatyuk suggested, then you could post a WM_CLOSE to the main window from your other thread.
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33502803
Any reason why the app needs to be a console app?
0
 

Author Comment

by:darrgyas
ID: 33502870
>>>> Any reason why the app needs to be a console app?
Yes, and it's a good one - but this is outside of the scope of my question.

Maybe it's not possible?
How would you send keys to the main thread in the console app from another thread? Maybe I'm doing it wrong?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 33503184
What about sending a CTRL_C_EVENT to ypour console handler to terminate?
#include <windows.h> 
#include <stdio.h> 
 
BOOL CtrlHandler( DWORD fdwCtrlType ) 
{ 
  switch( fdwCtrlType ) 
  { 
    // Handle the CTRL-C signal. 
    case CTRL_C_EVENT: 
      printf( "Ctrl-C event, exiting\n\n" );
      ExitProcess(0);
      return( TRUE );
 
    default: 
      return FALSE; 
  } 
} 
 
void main( void ) 
{ 
  if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) ) 
  { 
    printf( "\nThe Control Handler is installed.\n" ); 
 
    while( 1 ){ } 
  } 
  else 
    printf( "\nERROR: Could not set control handler"); 
}


// other thread:

  GenerateConsoleCtrlEvent(CTRL_C_EVENT,0);

Open in new window

0
 

Author Comment

by:darrgyas
ID: 33503322
>>>> jkr
Gives me a first-time exception, but it works. Not quite what I was looking for, but it is a legitimate solution.
Thank you - helpful as always.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

715 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