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.