{"id":498,"date":"2020-01-21T13:31:23","date_gmt":"2020-01-21T05:31:23","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=498"},"modified":"2025-02-26T11:35:23","modified_gmt":"2025-02-26T03:35:23","slug":"adding-console-i-o-to-a-win32-gui-app","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2020\/01\/21\/adding-console-i-o-to-a-win32-gui-app\/","title":{"rendered":"\u5411Win32 GUI\u7a0b\u5e8f\u6dfb\u52a0\u63a7\u5236I\/O"},"content":{"rendered":"<p>\u6700\u8fd1\u4f7f\u7528D3D11\u505a\u5f00\u5c0f\u8f66\u7684\u7f51\u6613-1\u4efb\u52a1, \u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\u662fWinMain\u51fd\u6570, \u65e0\u6cd5\u4f7f\u7528cout\u8fdb\u884c\u8c03\u8bd5, \u84dd\u7626\u9999\u83c7, \u4f7f\u7528\u5e38\u89c4\u7684\u9879\u76ee\u5c5e\u6027-\u914d\u7f6e\u5c5e\u6027-\u94fe\u63a5\u5668-\u7cfb\u7edf-\u5b50\u7cfb\u7edf-\u63a7\u5236\u53f0\u5927\u6cd5\u4f9d\u65e7\u65e0\u679c, \u7ec8\u4e8e\u5728stackoverflow\u89c5\u5f97\u826f\u65b9, \u6492\u82b1\u273f\u273f\u30fd(\u00b0\u25bd\u00b0)\u30ce\u273f<\/p>\n<p><!--more--><\/p>\n<p><strong>\u53c2\u8003\u6750\u6599<\/strong><br \/>\n1. <a href=\"https:\/\/stackoverflow.com\/questions\/191842\/how-do-i-get-console-output-in-c-with-a-windows-program\">How do I get console output in C++ with a Windows program?<\/a><br \/>\n2. <a href=\"https:\/\/www.cnblogs.com\/panlm\/p\/4387375.html\">winmain\u548cmain\u5165\u53e3\u51fd\u6570\u6bd4\u8f83<\/a><\/p>\n<p>\u9700\u8981\u8fdb\u884c\u91cd\u5b9a\u5411, \u867d\u7136\u6211\u4e5f\u4e0d\u5927\u61c2\u4ee3\u7801\u5177\u4f53\u5199\u4e86\u5565, \u56e7\u2026\u2026 \u53cd\u6b63\u80fd\u7528\u5c31\u884c, \u7ba1\u5b83\u54a7233333<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n#ifndef __GUICON_H__\r\n#define __GUICON_H__\r\n#ifdef _DEBUG\r\n\r\n#include &lt;iostream&gt;\r\n\r\nvoid RedirectIOToConsole();\r\n\r\n#endif\r\n#endif\r\n\r\n\/\/ End of File\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n#include &lt;windows.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;fcntl.h&gt;\r\n#include &lt;io.h&gt;\r\n#include &lt;fstream&gt;\r\n#include &lt;guicon.h&gt;\r\n#ifndef _USE_OLD_IOSTREAMS\r\nusing namespace std;\r\n#endif\r\n\/\/ maximum mumber of lines the output console should have\r\nstatic const WORD MAX_CONSOLE_LINES = 500;\r\n#ifdef _DEBUG\r\nvoid RedirectIOToConsole()\r\n{\r\n\tint hConHandle;\r\n\tlong lStdHandle;\r\n\tCONSOLE_SCREEN_BUFFER_INFO coninfo;\r\n\tFILE *fp;\r\n\r\n\t\/\/ allocate a console for this app\r\n\tAllocConsole();\r\n\r\n\t\/\/ set the screen buffer to be big enough to let us scroll text\r\n\tGetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);\r\n\tconinfo.dwSize.Y = MAX_CONSOLE_LINES;\r\n\tSetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);\r\n\r\n\t\/\/ redirect unbuffered STDOUT to the console\r\n\tlStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);\r\n\thConHandle = _open_osfhandle(lStdHandle, _O_TEXT);\r\n\tfp = _fdopen(hConHandle, \"w\");\r\n\t*stdout = *fp;\r\n\tsetvbuf(stdout, NULL, _IONBF, 0);\r\n\r\n\t\/\/ redirect unbuffered STDIN to the console\r\n\tlStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);\r\n\thConHandle = _open_osfhandle(lStdHandle, _O_TEXT);\r\n\tfp = _fdopen(hConHandle, \"r\");\r\n\t*stdin = *fp;\r\n\tsetvbuf(stdin, NULL, _IONBF, 0);\r\n\r\n\t\/\/ redirect unbuffered STDERR to the console\r\n\tlStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);\r\n\thConHandle = _open_osfhandle(lStdHandle, _O_TEXT);\r\n\tfp = _fdopen(hConHandle, \"w\");\r\n\t*stderr = *fp;\r\n\tsetvbuf(stderr, NULL, _IONBF, 0);\r\n\r\n\t\/\/ make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog\r\n\t\/\/ point to console as well\r\n\tios::sync_with_stdio();\r\n\r\n\t\/\/ Get STDOUT handle\r\n\tHANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);\r\n\tint SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);\r\n\tFILE *COutputHandle = _fdopen(SystemOutput, \"w\");\r\n\r\n\t\/\/ Get STDERR handle\r\n\tHANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);\r\n\tint SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);\r\n\tFILE *CErrorHandle = _fdopen(SystemError, \"w\");\r\n\r\n\t\/\/ Get STDIN handle\r\n\tHANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);\r\n\tint SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);\r\n\tFILE *CInputHandle = _fdopen(SystemInput, \"r\");\r\n\r\n\t\/\/make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well\r\n\tios::sync_with_stdio(true);\r\n\r\n\t\/\/ Redirect the CRT standard input, output, and error handles to the console\r\n\tfreopen_s(&CInputHandle, \"CONIN$\", \"r\", stdin);\r\n\tfreopen_s(&COutputHandle, \"CONOUT$\", \"w\", stdout);\r\n\tfreopen_s(&CErrorHandle, \"CONOUT$\", \"w\", stderr);\r\n\r\n\t\/\/Clear the error state for each of the C++ standard stream objects. We need to do this, as\r\n\t\/\/attempts to access the standard streams before they refer to a valid target will cause the\r\n\t\/\/iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems\r\n\t\/\/to always occur during startup regardless of whether anything has been read from or written to\r\n\t\/\/the console or not.\r\n\tstd::wcout.clear();\r\n\tstd::cout.clear();\r\n\tstd::wcerr.clear();\r\n\tstd::cerr.clear();\r\n\tstd::wcin.clear();\r\n\tstd::cin.clear();\r\n}\r\n\r\n#endif\r\n\/\/End of File\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n#include \"guicon.h\"\r\n\r\nint WINAPI WinMain(HINSTANCE hinstance,\r\n\tHINSTANCE prevInstance,\r\n\tPSTR cmdLine,\r\n\tint showCmd)\r\n{\r\n#ifdef _DEBUG\r\n\tRedirectIOToConsole();\r\n#endif\r\n\r\n        std::cout&lt;&lt;\"test\"&lt;&lt;std::endl;\r\n}\r\n<\/pre>\n<p><strong>\u6ce8\u8bb01: <\/strong>WinMain\u51fd\u6570\u63a5\u65364\u4e2a\u53c2\u6570, \u8fd9\u4e9b\u53c2\u6570\u90fd\u662f\u5728\u7cfb\u7edf\u8c03\u7528WinMain\u51fd\u6570\u65f6, \u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u7684.<br \/>\n\u7b2c\u4e00\u4e2a\u53c2\u6570hInstance\u8868\u793a\u8be5\u7a0b\u5e8f\u5f53\u524d\u8fd0\u884c\u7684\u5b9e\u4f8b\u7684\u53e5\u67c4, \u8fd9\u662f\u4e00\u4e2a\u6570\u503c. \u5f53\u7a0b\u5e8f\u5728Windows\u4e0b\u8fd0\u884c\u65f6, \u5b83\u552f\u4e00\u6807\u8bc6\u8fd0\u884c\u4e2d\u7684\u5b9e\u4f8b(\u6ce8\u610f, \u53ea\u6709\u8fd0\u884c\u4e2d\u7684\u7a0b\u5e8f\u5b9e\u4f8b, \u624d\u6709\u5b9e\u4f8b\u53e5\u67c4), \u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u8fd0\u884c\u591a\u4e2a\u5b9e\u4f8b, \u6bcf\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b, \u7cfb\u7edf\u90fd\u4f1a\u7ed9\u8be5\u5b9e\u4f8b\u5206\u914d\u4e00\u4e2a\u53e5\u67c4\u503c, \u5e76\u901a\u8fc7hInstance\u53c2\u6570\u4f20\u9012\u7ed9WinMain\u51fd\u6570.<br \/>\n\u7b2c\u4e8c\u4e2a\u53c2\u6570hPrevInstance(MSDN) \u5728Win32\u73af\u5883\u4e0b, \u8fd9\u4e2a\u53c2\u6570\u4e0d\u8d77\u4f5c\u7528, \u5386\u53f2\u9057\u7559, hPrevInstance=NULL.<br \/>\n\u7b2c\u4e09\u4e2a\u53c2\u6570lpCmdLine\u662f\u4e00\u4e2a\u4ee5\u7a7a(&#8216;\\0&#8217;) \u7ec8\u6b62\u7684\u5b57\u7b26\u4e32, \u6307\u5b9a\u4f20\u9012\u7ed9\u5e94\u7528\u7a0b\u5e8f\u7684\u547d\u4ee4\u884c\u53c2\u6570.<\/p>\n<p><strong>\u6ce8\u8bb02: <\/strong>\u8fd0\u884c\u53c2\u6570, \u4f8b\u5982\u5728\u547d\u4ee4\u884c\u952e\u5165: test.exe \/install, \u90a3\u4e48\u7a0b\u5e8f\u5165\u53e3WinMain\u5904, \u5176\u53c2\u6570lpCmdLine\u5c31\u63a5\u6536\u5230\u4e86\/install\u53c2\u6570. \u4f46\u662f, \u81f3\u4e8e\u4f60\u7528\u8fd9\u4e2a\u53c2\u6570\u505a\u4ec0\u4e48, \u90a3\u662f\u7528\u4ee3\u7801\u5b9e\u73b0. \u800c\u5b9e\u73b0\u5b89\u88c5, \u8fd9\u9700\u8981\u6839\u636e\u4f60\u7684\u9700\u6c42\u52a0\u5165\u4ee3\u7801, \u548c\u547d\u4ee4\u884c\u53c2\u6570\u65e0\u5173. \u8fd9\u4e2a\u8ddf\u547d\u4ee4\u884c\u7684int main(int argc, char* argv[])\u4e0d\u540c.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4f7f\u7528D3D11\u505a\u5f00\u5c0f\u8f66\u7684\u7f51\u6613-1\u4efb\u52a1, \u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\u662fWinMain\u51fd\u6570, \u65e0\u6cd5\u4f7f\u7528cout\u8fdb\u884c\u8c03\u8bd5,  &hellip; <a href=\"https:\/\/www.caiqinyi.cn\/index.php\/2020\/01\/21\/adding-console-i-o-to-a-win32-gui-app\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u5411Win32 GUI\u7a0b\u5e8f\u6dfb\u52a0\u63a7\u5236I\/O<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,16],"tags":[],"_links":{"self":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/498"}],"collection":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=498"}],"version-history":[{"count":15,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/498\/revisions"}],"predecessor-version":[{"id":3651,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/498\/revisions\/3651"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}