Pelles C forum

C language => Tips & tricks => Topic started by: TimoVJL on December 03, 2025, 01:21:48 PM

Title: Testing Console
Post by: TimoVJL on December 03, 2025, 01:21:48 PM
Just a stupid test:#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")

void __cdecl mainCRTStartup(void)
{
char szTmp[100], szTmp2[100];
HANDLE hConWnd = GetConsoleWindow();
HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);

int nLen = wsprintf(szTmp, "hConWnd %ph\n", hConWnd);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);
//FlashWindow(hConWnd, TRUE);
GetConsoleTitle(szTmp2, sizeof(szTmp2));
nLen = wsprintf(szTmp, "CTitle: %s\n", szTmp2);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);

GetWindowText(hConWnd, szTmp2, sizeof(szTmp2)); // don't work in Windows 11
nLen = wsprintf(szTmp, "WTitle: %s\n", szTmp2);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);

HWND hWnd = FindWindow(NULL, szTmp2);
nLen = wsprintf(szTmp, "hWnd %ph\n", hWnd);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);

LONG_PTR lp = GetWindowLongPtr(hConWnd, GWLP_WNDPROC);
nLen = wsprintf(szTmp, "WndProc %ph\n", (void*)lp);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);

lp = GetWindowLongPtr(hWnd, GWLP_WNDPROC);
nLen = wsprintf(szTmp, "WndProc %ph\n", (void*)lp);
WriteConsole(hConOut, szTmp, nLen, NULL, 0);

//PostMessage(hConWnd, WM_QUIT, 0, 0); // quit console and program stay running
//SendMessage(hConWnd, WM_QUIT, 0, 0); // do nothing

// stay running
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)) // do nothing
//while(PeekMessage(&msg, NULL, 0, 0, 0)) // end program
{
//nLen = wsprintf("hwnd %ph\n", msg.hwnd);
//WriteConsole(hConOut, szTmp, nLen, NULL, 0);
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ExitProcess(0);
}
Title: Re: Testing Console
Post by: Robert on December 03, 2025, 11:49:01 PM
Compiled on Linux Wine with Pelles C.

Executed. Too stupid.

> wine ./StupidTest.exe
hConWnd 000000000002004Eh
CTitle:
WTitle:
hWnd 000000000001004Ah
WndProc 0000000000000000h
WndProc 0000000000000000h
Title: Re: Testing Console
Post by: TimoVJL on December 04, 2025, 01:15:02 AM
so GetConsoleTitle() don't work in wine cmd.exe
also GetWindowLongPtr(hConWnd, GWLP_WNDPROC); fails.

And yes, a stupid test was made for Windows 7 and Windows 1x cmd.exe.
Title: Re: Testing Console
Post by: Vortex on December 04, 2025, 10:06:32 PM
Hi Timo,

What is the exact version of your Windows 11 installation ? For example, is it Windows 11 2024 H2 ?
Title: Re: Testing Console
Post by: TimoVJL on December 04, 2025, 10:35:47 PM
Windows 11 Home 25H2
Title: Re: Testing Console
Post by: Vortex on December 05, 2025, 09:48:19 AM
Hi Timo,

Here is my report. Operating system at work : Windows 11 2024 H2

D:\PellesC\ConsoleTest>ConsoleTest.exe
hConWnd 00000000000708E8h
CTitle: Administrator: C:\Windows\System32\cmd.exe - ConsoleTest.exe
WTitle:
hWnd 0000000000010174h
WndProc 0000000000000000h
WndProc 0000000000000000h

Title: Re: Testing Console
Post by: TimoVJL on December 05, 2025, 10:46:12 AM
Those Windows console variations are interesting.
It seems, that they don't have normal message loop for user.
A console window handle isn't real windows handle at all.

That test is stupid, as it don't handle any errors, like empty window handle.
Title: Re: Testing Console
Post by: John Z on December 06, 2025, 11:00:38 AM
Quote from: TimoVJL on December 04, 2025, 10:35:47 PMWindows 11 Home 25H2


I think you are now ahead of most of the rest, including me....  good hunting!

John Z
Title: Re: Testing Console
Post by: Vortex on December 06, 2025, 09:50:11 PM
Hi Timo,

Your test is not meaningless. Curiosity is what keeps us alive.