gpt4 book ai didi

c++ - 从 Windows 10 上的 Win32 GUI 应用程序输出到控制台

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:12:43 28 4
gpt4 key购买 nike

我尝试将这段代码输出到控制台:

#include <Windows.h>

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
AllocConsole();

HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 1);
*stdout = *hf_out;

HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 128);
*stdin = *hf_in;

printf("Hello!");
}

控制台打开但没有任何输出。该代码有什么问题?

我尝试了所有这些建议:

https://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/

http://dslweb.nwnexus.com/~ast/dload/guicon.htm

How do I print to the debug output window in a Win32 app?

但我无法在 WinMain 的 Windows 10 上使用 AllocConsole() 创建的控制台获得任何输出。注意:我实际上并没有创建任何真正的窗口。Window 10 中是否发生了某些更改,导致上述解决方案无法正常工作,或者是否有我可能遗漏的内容(编译器标志或其他内容)?

你怎么看?

最佳答案

基于 ProXicT 链接的已接受答案,并进行了一些修改。以下代码适用于 std::cout。其他方法不适用于 64 位的 Visual Studio 2015:

#include <iostream>
#include <cstdio>
#include <fstream>

#include <Windows.h>


// For debugging
#include <io.h>
#include <fcntl.h>


#define UNUSED(x) (void)(x) // Unused param (C compatible - not applicable to expressions)

class outbuf : public std::streambuf {
public:
outbuf() {
setp(0, 0);
}

virtual int_type overflow(int_type c = traits_type::eof()) {
return fputc(c, stdout) == EOF ? traits_type::eof() : c;
}
};

int CALLBACK
WinMain (HINSTANCE hInstance,
HINSTANCE /*hPrevInst*/, // Unused param (C++ only)
LPSTR lpCmdLine,
int (nShowCmd))
{
UNUSED(hInstance);
// UNUSED(hPrevInst);
UNUSED(lpCmdLine);
UNUSED(nShowCmd); // This param is used


// create the console
if (AllocConsole()) {
FILE* pCout;
freopen_s(&pCout, "CONOUT$", "w", stdout);
SetConsoleTitle(L"Debug Console");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
}

// set std::cout to use my custom streambuf
outbuf ob;
std::streambuf *sb = std::cout.rdbuf(&ob);

// do some work here
printf("Hello!\n");

std::cout << "nShowCmd = " << nShowCmd << std::endl;
std::cout << "Now making my first Windows window!" << std::endl;


// make sure to restore the original so we don't get a crash on close!
std::cout.rdbuf(sb);

MessageBoxW (NULL,
L"Hello World!",
L"hello",
MB_OK | MB_ICONINFORMATION);

return 0;
}

编辑:

与上面相同的内容,但为了完整性使用了颜色:

#include <iostream>
#include <cstdio>
#include <fstream>

#include <Windows.h>


// For debugging
#include <io.h>
#include <fcntl.h>


#define UNUSED(x) (void)(x) // Unused param (C compatible - not applicable to expressions)

class outbuf : public std::streambuf {
public:
outbuf() {
setp(0, 0);
}

virtual int_type overflow(int_type c = traits_type::eof()) {
return fputc(c, stdout) == EOF ? traits_type::eof() : c;
}
};

int CALLBACK
WinMain (HINSTANCE hInstance,
HINSTANCE /*hPrevInst*/, // Unused param (C++ only)
LPSTR lpCmdLine,
int (nShowCmd))
{
UNUSED(hInstance);
// UNUSED(hPrevInst);
UNUSED(lpCmdLine);
UNUSED(nShowCmd); // This param is used


// create the console
if (AllocConsole()) {
FILE* pCout;
freopen_s(&pCout, "CONOUT$", "w", stdout);
SetConsoleTitle(L"Debug Console");

}

// set std::cout to use my custom streambuf
outbuf ob;
std::streambuf *sb = std::cout.rdbuf(&ob);

// do some work here

printf("Hello!\n");

HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD defaultConsoleTextAttributes;
GetConsoleScreenBufferInfo(stdHandle, &consoleInfo);
defaultConsoleTextAttributes = consoleInfo.wAttributes;
WORD currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "nShowCmd = " << nShowCmd << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;
currentConsoleTextAttributes = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
SetConsoleTextAttribute(stdHandle, currentConsoleTextAttributes);
std::cout << "Now making my first Windows window!" << std::endl;



// make sure to restore the original so we don't get a crash on close!
std::cout.rdbuf(sb);

Sleep(5000);
ShowWindow(GetConsoleWindow(), SW_HIDE);
FreeConsole();

MessageBoxW (NULL,
L"Hello World!",
L"hello",
MB_OK | MB_ICONINFORMATION);

return 0;
}

现在剩下的就是把它做成一个完整的日志类。

关于c++ - 从 Windows 10 上的 Win32 GUI 应用程序输出到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32185512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com