PROWAREtech








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);
}
Comment