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.
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.
Table A:Console application differences between Windows 95 and Windows NT
| Windows 95 | Windows NT | |
| Console window class name | tty | ConsoleWindowClass |
| Console window caption | Program name only | Full 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.
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 Win95With this code snippet, you can find a console window regardless of whether the user is running Windows 95 or Windows NT.
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.