• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1945
  • Last Modified:

GetMessage() in MFC console app

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
darrgyas
Asked:
darrgyas
  • 4
  • 3
  • 2
  • +1
1 Solution
 
jkrCommented:
Well, what are you trying to achieve?
0
 
darrgyasAuthor Commented:
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
 
Anthony2000Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
pgnatyukCommented:
>> 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
 
darrgyasAuthor Commented:
>>>> 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
 
pgnatyukCommented:
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
 
Anthony2000Commented:
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
 
Anthony2000Commented:
Any reason why the app needs to be a console app?
0
 
darrgyasAuthor Commented:
>>>> 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
 
jkrCommented:
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
 
darrgyasAuthor Commented:
>>>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now