July 1997

Finding a console window

by Kent Reisdorph

If you write console applications, you may occasionally need to determine the window handle (HWND) of your console application. You may even need the window handle of another application's console window. With that information, you can shut down the console window, move it, hide it, or perform any of a number of other operations.

In this article, we'll explain how to obtain the window handle of a console application. We'll demonstrate how to use the FindWindow() function and some of the tricks involved in finding console windows in both Windows 95 and Windows NT.

FindWindow

You can use the FindWindow() Windows API function to obtain the window handle of any window--main application windows, controls, dialog boxes, and, of course, console windows. The syntax for FindWindow() is as follows:
HWND hWnd = FindWindow("MyWindowClass", "My Program");
The first parameter specifies the class name of the window, and the second parameter specifies the window's text--typically, the window's caption.

If the second parameter is NULL, then the function will find any window with the specified class name. For this reason, you should always specify the window text parameter unless you know that the window you're looking for has a unique class name.

A console application has an associated window. So, you can use FindWindow() to obtain the window handle of the console application just as you'd use it in a conventional GUI Windows program. The trick is to supply the proper class name and window text.

Making a difference

It probably comes as no surprise that Microsoft handles some console-window issues differently in Windows 95 than in Windows NT 4--in particular, the class name and the console window's caption. Table A illustrates these differences. In order to account for these variations, you must check to see which operating system your application is running under, and then take the appropriate steps to find the console window handle.

Table A:Console application differences between Windows 95 and Windows NT
 Windows 95Windows NT
Console window class namettyConsoleWindowClass
Console window captionProgram name onlyFull path, filename, and extension

Once you determine which operating system you're using, you can call FindWindow() with the appropriate parameters. As I hinted before, just knowing the class name usually isn't good enough to ensure that you'll locate the correct window. For example, the user may have several console applications or command prompt boxes open at any given time. Supplying only the class name will cause FindWindow() to return the window handle of the first console window it encounters--which may or may not be the one you're looking for.

Where am I?

To determine which operating system you're running under, you can use the Windows API function GetVersionEx(). This function returns information about the operating system in a OSVERSIONINFO structure. VCL defines the typedef TOSVersionInfo, so you can use that if you like.

Before you call GetVersionEx(), you must create an instance of the TOSVersionInfo structure and set the dwOSVersionInfoSize member of the structure to the size of the structure itself. For example, you can use code like the following:

TOSVersionInfo info;
info.dwOSVersionInfoSize = sizeof(info);
GetVersionEx(&info);
Now the TOSVersionInfostructure contains information about the operating system currently in use. The only data member of this structure that you care about in this case is dwPlatformId. This data member will contain the value
VER_PLATFORM_WIN32_WINDOWS

if the user is running Windows 95, and the value

VER_PLATFORM_WIN32_NT

if the user is running Windows NT.

Armed with that operating system knowledge, you can write code like the following:

if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  // running NT
else 
  // running Win95
With this code snippet, you can find a console window regardless of whether the user is running Windows 95 or Windows NT.

An example

Listing A contains a function you can use to determine the window handle of a console application. This function detects the OS version, then calls FindWindow() with the appropriate parameters. The function returns the result of the FindWindow() function--either the window handle of the console window, if the function found the window, or NULL if the function couldn't find the window.

Listing A: GetConsoleHWND() Function

// This routine gets the window handle of
// a console window.
HWND GetConsoleHWND()
{
  char title[256] = "";
  char className[20] = "";
  // Get the name of this executable.
  GetModuleFileName(0, title, sizeof(title));
  // See if we're running on Win95 or NT.
  TOSVersionInfo info;
  info.dwOSVersionInfoSize = sizeof(info);
  GetVersionEx(&info);
  int platform = info.dwPlatformId;
  // If NT then the class name is 'ConsoleWindowClass'
  // The title is the entire path and filename.
  if (platform == VER_PLATFORM_WIN32_NT)
    strcpy(className, "ConsoleWindowClass");
    // If Win95 then the class name is 'tty' and
    // the window title is just the filename
    // without path and extension.
  else {
    strcpy(className, "tty");
    // Strip off everything but the filename.
    std::string name = title;
    int pos = name.find_last_of("\\");
    name.remove(0, ++pos);
    name.remove(name.length() - 4, 4);
   strcpy(title, name.c_str());
  }
  // Return the result of FindWindow().
  return FindWindow(className, title);
}
Kent Reisdorph is a editor of the C++Builder Developer's Journal as well as director of systems and services at TurboPower Software Company, and a member of TeamB, Borland's volunteer online support group. He's the author of Teach Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days. You can contact Kent at editor@bridgespublishing.com.