PROWAREtech

articles » current » windows » application-programming-interface » get-console-window-handle

Windows API: Get Console Window Handle (HWND)

This mostly applies to older versions of Windows though it works with newer versions just as well; written in C.

This code will get the HWND/handle of a console window (only applicable to console applications).

#include <windows.h>

HWND GetConsoleHandle()
{
	#define BUFFSIZE 768
	HWND hwnd;
	char pszWindowTitle[BUFFSIZE]; 

	GetConsoleTitle(pszWindowTitle, BUFFSIZE);
	int i = strlen(pszWindowTitle);
	_itoa(GetTickCount(), &pszWindowTitle[i], 16);
	_itoa(GetCurrentProcessId(), &pszWindowTitle[strlen(pszWindowTitle)], 16);
	SetConsoleTitle(pszWindowTitle);
	Sleep(50);
	hwnd = FindWindow(NULL, pszWindowTitle);
	pszWindowTitle[i] = 0;
	SetConsoleTitle(pszWindowTitle);
	return(hwnd);
}

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE