How To Obtain a Console Window Handle (HWND)

This article was previously published under Q124103
It may be useful to manipulate a window associated with a consoleapplication. The Win32 API provides no direct method for obtaining thewindow handle associated with a console application. However, you canobtain the window handle by calling FindWindow(). This function retrieves awindow handle based on a class name or window name.

Call GetConsoleTitle() to determine the current console title. Then supplythe current console title to FindWindow().
Because multiple windows may have the same title, you should change thecurrent console window title to a unique title. This will help prevent thewrong window handle from being returned. Use SetConsoleTitle() to changethe current console window title. Here is the process:
  1. Call GetConsoleTitle() to save the current console window title.
  2. Call SetConsoleTitle() to change the console title to a unique title.
  3. Call Sleep(40) to ensure the window title was updated.
  4. Call FindWindow(NULL, uniquetitle), to obtain the HWND this call returns the HWND -- or NULL if the operation failed.
  5. Call SetConsoleTitle() with the value retrieved from step 1, to restore the original window title.
You should test the resulting HWND. For example, you can test to see if thereturned HWND corresponds with the current process by callingGetWindowText() on the HWND and comparing the result withGetConsoleTitle().

The resulting HWND is not guaranteed to be suitable for all window handleoperations.

Sample Code

The following function retrieves the current console application windowhandle (HWND). If the function succeeds, the return value is the handle ofthe console window. If the function fails, the return value is NULL. Someerror checking is omitted, for brevity.
   HWND GetConsoleHwnd(void)   {       #define MY_BUFSIZE 1024 // Buffer size for console window titles.       HWND hwndFound;         // This is what is returned to the caller.       char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated                                           // WindowTitle.       char pszOldWindowTitle[MY_BUFSIZE]; // Contains original                                           // WindowTitle.       // Fetch current window title.       GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);       // Format a "unique" NewWindowTitle.       wsprintf(pszNewWindowTitle,"%d/%d",                   GetTickCount(),                   GetCurrentProcessId());       // Change current window title.       SetConsoleTitle(pszNewWindowTitle);       // Ensure window title has been updated.       Sleep(40);       // Look for NewWindowTitle.       hwndFound=FindWindow(NULL, pszNewWindowTitle);       // Restore original window title.       SetConsoleTitle(pszOldWindowTitle);       return(hwndFound);   }				

Article ID: 124103 - Last Review: 07/11/2005 22:34:00 - Revision: 1.3

Microsoft Platform Software Development Kit-January 2000 Edition

  • kbhowto kbwndw KB124103