如何使用纯WIN32 API调用为GUI应用程序创建调试控制台
我正在为WIN32 GUI应用程序创建一个调试控制台,我正试图以不同的方式做到这一点,而我的生活中我无法让这个工作,这是一个简单的事情,但它正在驱动我疯了。我知道这已被问过一百万次,一般的方式和我一直在使用的是:
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");
我想太多的空闲时间/无聊/好奇心,或者有些奇怪的意愿以尽可能最难的方式做事,我想要尝试使用AllocConsole,CreateFile,SetStdHandle进行纯粹的WIN32调用。不管谷歌搜索多少或搜索MSDN,我都无法正常工作。所有功能都返回成功,控制台在屏幕上打开,但没有输入/ ouput。
我尝试过:
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);
如果我是printf()或std :: cout,则不会显示任何内容。
任何帮助都会很棒。
谢谢
Matt
if I printf() or std::cout, nothing is displayed.
Any help would be great.
Thanks
Matt
,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_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);
", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); SetStdHandle(STD_OUTPUT_HANDLE, hConsoleSTDOUT);
如果我是printf()或std :: cout,则不会显示任何内容。
任何帮助都会很棒。
谢谢
Matt
if I printf() or std::cout, nothing is displayed.
Any help would be great.
Thanks
Matt
我使用Console窗口来调试我的所有应用程序,无论是否有GUI。
I在大多数情况下使用纯Win32 API。
参见这篇文章 [ ^ ]
不仅可以,而且你可以设置颜色和字体样式。
首先,你打电话:
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);
}
然后,只需使用 wprintf()即可显示文字。
如果您愿意要清除显示区域,只需致电:
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);
}
}