Using dialogs in console apps

Home
Back To Tips Page

Traditionally, the world has been divided into two kinds of apps, and ne'er the twain shall meet: Console applications and GUI applications. (Actually, if you write a console app that talks to a scanner, and a GUI app that talks to a scanner, they both meet the TWAIN...)

Strictly speaking, this separation has not been true. A console app can presumably create a message loop and pop up windows; a GUI app can create and attach a console. But these are considered major pieces of effort.

A popular reason GUI apps seem to want to create consoles is to have a logging window to which debug output can be written. I solved this problem years ago with a technology that has evolved into a fairly sophisticated set of MFC classes, my Logging ListBox class. 

However, last week a client contacted me. He needed the ability to handle a MessageBox from a console app, and I had everything working with one little, but showstopper, glitch: I couldn't find any API that gave me a handle to the window in which the console app was running.

So I did the obvious thing: I posted the question on the microsoft.public.vc.mfc newsgroup where I hang out. Sure enough, David Lowndes replied within minutes with the solution, which I have incorporated into the answer below. Thanks, and a Tip Of The Flounder Fin to David.

David's suggestion was to use SetConsoleTitle to set a window title for the window, then use FindWindow to find it. In order to guarantee the name is unique, he also suggested using a GUID (my favorite technique) for the window name, since that (almost) guarantees that no other window will have that name. Because we could have multiple instances of the program running, we additionally add the current process ID, which now does guarantee that the window name will be unique.

HWND FindConsoleHandle()
 {
  static LPCTSTR temptitle = _T("{98C1C303-2A9E-11d4-9FF5-006067718D04}");
  TCHAR title[512];
  TCHAR me[64];
  wsprintf(me, _T("%s-%08x"), temptitle, GetCurrentProcessID));
  if(GetConsoleTitle(title, sizeof(title)/sizeof(TCHAR)) == 0)
    return NULL;
  SetConsoleTitle(me);
  HWND wnd = FindWindow(NULL, me);
  SetConsoleTitle(title);
  return wnd;
 }

To invoke the MessageBox, just use the HWND returned from this function as the HWND for the parent window.

For example, I am now able to execute the following in a plain-vanilla console app:

int response = MessageBox( FindConsoleHandle(), 
                           _T("File read error, continue?"), 
                           _T("File Error"), 
                           MB_ICONERROR | MB_YESNO);

Be sure to replace the GUID above with a GUID of your own that you get from GUIDGEN; otherwise you lose the desired uniqueness.

This should work equally well for operations like standard dialogs such as File Open, File Save, and even custom dialogs of your own. Note that it won't work for modeless dialogs, because they don't have their own message pump.

If you need the module instance, use GetModuleHandle(NULL) to get the module handle (which translates to the instance) to create a dialog, for example, 

HANDLE dlg = CreateDialog(GetModuleHandle(NULL), IDD_MYDLG, 
                          FindConsoleHandle(), MyDlgProc);

Change Log

8-Jan-03: Added process ID to the unque name. 

[Dividing Line Image]

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to newcomer@flounder.com with questions or comments about this web site.
Copyright 2000-2003, The Joseph M. Newcomer Co. All Rights Reserved.
Last modified: May 14, 2011