Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm playing around with creating a debug console on for a WIN32 GUI app and I'm trying to do it a different way and by the life of me I cannot get this working, its such a simple thing but it's driving me crazy. I know this has been asked a million times before and the general way and what I have been using is this:
AllocConsole();

freopen("CON", "r", stdin);
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);

or also
OutputDebugString(TEXT("blah, blah blah");


I guess too much free time/boredom/curiosity, or some strange will to do things the hardest way possible, I wanted to try doing it differently with just pure WIN32 calls with AllocConsole, CreateFile, SetStdHandle. No matter how much googling or searching through MSDN, I cant get this to work correctly. All the functions return successful and console opens up on the screen, but no input/ouput.

What I have tried:

AllocConsole();

HANDLE hConsoleSTDIN = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
                                  FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);

SetStdHandle(STD_INPUT_HANDLE, hConsoleSTDIN);

HANDLE hConsoleSTDOUT = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
                                   FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);

SetStdHandle(STD_OUTPUT_HANDLE, hConsoleSTDOUT);

if I printf() or std::cout, nothing is displayed.

Any help would be great.
Thanks
Matt
Posted
Updated 15-Apr-19 17:47pm
v2
Comments
11917640 Member 16-Apr-19 1:04am    
You can change the /SUBSYSTEM parameter from Windows to Console (if this causes linker error, change also /ENTRY parameter). Or just use OutputDebugString + DebugView https://docs.microsoft.com/en-us/sysinternals/downloads/debugview

I use a Console window to debug all of my applications, with or without GUI.
I use pure Win32 API in most of the cases.
See This article[^]
Not only that it possible but you can set colors and font styles.
First, you call:

#include <conio.h>
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

To set the text color, I use:
inline void setcolor(int textcol, int backcol)
{
	if ((textcol % 16) == (backcol % 16))textcol++;
	textcol %= 16; backcol %= 16;
	unsigned short wAttributes = ((unsigned)backcol << 4) | (unsigned)textcol;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	SetConsoleTextAttribute(hConsole, wAttributes);
}

Then, just use wprintf() to display the text.
If you wish to clear the display area, just call:
system("cls");

Also refresh the display by calling:
void refresh()
{
	HWND hwnd = FindWindowEx(NULL, NULL, L"CabinetWClass", NULL);
	while (hwnd != NULL)
	{
		PostMessage(hwnd, WM_COMMAND, 41504, 0);
		hwnd = FindWindowEx(NULL, hwnd, L"CabinetWClass", NULL);
	}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900