Click here to Skip to main content
11,706,477 members (47,652 online)
Click here to Skip to main content

Using dialogs in console apps

, 23 May 2000 101.3K 32
Rate this:
Please Sign up or sign in to vote.
Learn how to display a message box from a console application.

Introduction

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 guarantees that no other window will have that name!

HWND FindConsoleHandle()
{
   static LPCTSTR temptitle = _T("{98C1C303-2A9E-11d4-9FF5-006067718D04}");
   TCHAR title[512];
   if(GetConsoleTitle(title, sizeof(title)/sizeof(TCHAR)) == 0)
      return NULL;
   SetConsoleTitle(temptitle);
   HWND wnd = FindWindow(NULL, temptitle);
   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.


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 © 1999 <!--webbot bot="Substitution" s-variable="CompanyLongName" startspan -->CompanyLongName<!--webbot bot="Substitution" endspan i-checksum="22147" --> All Rights Reserved.
www.flounder.com/mvp_tips.htm

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Joseph M. Newcomer
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

You may also be interested in...

Comments and Discussions

 
GeneralThere is always an easier way Pin
bdawson@newcorp.com22-Feb-06 8:26
memberbdawson@newcorp.com22-Feb-06 8:26 
General? What about the other way around Pin
Synetech4-Dec-03 18:18
memberSynetech4-Dec-03 18:18 
GeneralLet me know too, please! Pin
Anonymous3-Apr-04 2:44
sussAnonymous3-Apr-04 2:44 
GeneralRe: Let me know too, please! Pin
baker_tony3-Apr-04 2:46
memberbaker_tony3-Apr-04 2:46 
GeneralRe: ? What about the other way around Pin
Joseph M. Newcomer3-Apr-04 7:13
memberJoseph M. Newcomer3-Apr-04 7:13 
GeneralRe: ? What about the other way around Pin
Wasteland76-May-04 21:24
memberWasteland76-May-04 21:24 
GeneralRe: ? What about the other way around Pin
cowsonfire22-Feb-06 1:42
membercowsonfire22-Feb-06 1:42 
http://cowsonfire.com/runtest.cpp
GeneralBeeter way to get HWND of parent Pin
MLoibl16-Feb-01 8:00
memberMLoibl16-Feb-01 8:00 
Generala missing thing Pin
MLoibl16-Feb-01 22:58
memberMLoibl16-Feb-01 22:58 
GeneralRe: Beeter way to get HWND of parent Pin
Lim Bio Liong8-Jan-02 20:07
memberLim Bio Liong8-Jan-02 20:07 
GeneralRe: Beeter way to get HWND of parent Pin
Wasteland76-May-04 21:20
memberWasteland76-May-04 21:20 
GeneralYou don't need all this code Pin
landshark7-Jun-00 3:55
susslandshark7-Jun-00 3:55 
GeneralRe: You don't need all this code Pin
Gene18-Jul-00 7:34
sussGene18-Jul-00 7:34 
GeneralEast is east... Pin
Ravi Bhavnani6-Jun-00 9:18
sussRavi Bhavnani6-Jun-00 9:18 
GeneralRe: dialogs in console apps Pin
Gisle Vanem24-May-00 22:05
sussGisle Vanem24-May-00 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 24 May 2000
Article Copyright 2000 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid