简体   繁体   English

如何在Windows(C ++ WinApi)的“ SUBSYSTEM:WINDOWS”下显示并输出到控制台(cmd)

[英]How to show and output to the console (cmd) on Windows (C++ WinApi) under `SUBSYSTEM:WINDOWS`

I have seen windows applications that operate with both a Window and a console. 我已经看到了可同时与Window和控制台一起使用的Windows应用程序。 I find myself wondering about this every now and then and I remember whenever I do research it I never find a solution. 我发现自己时不时地对此感到疑惑,并且记得当我进行研究时始终找不到解决方案。

Is there a way to program my WinApi C++ application such that I can output to the console as I would normally do when under the linker option SUBSYSTEM:CONSOLE ? 有没有一种方法可以对WinApi C ++应用程序进行编程,以便可以在链接器选项SUBSYSTEM:CONSOLE下像往常一样输出到SUBSYSTEM:CONSOLE In other words I can have a Window open, with my WinMain and WndProc etc. but I would also like to do std::cout , std::cin and std::wcout .. A good use-case for that, would be to use the console for logging stuff and the Window as the main application. 换句话说,我可以用WinMainWndProc等打开一个窗口,但是我也想做std::coutstd::cinstd::wcout 。一个很好的用例是使用控制台记录内容,并使用Window作为主要应用程序。

Would It be a good idea to use std::system() instead and launch cmd from that and then output? 改用std::system()并从中启动cmd然后输出是个好主意吗? I don't think so, but of course I don't know. 我不这么认为,但是我当然不知道。

Windows 8.1, x86_64, MSVS 2017. Windows 8.1,x86_64,MSVS 2017。

Use AttachConsole to attach to an existing console, or AllocConsole to create a new console. 使用AttachConsole附加到现有控制台,或使用AllocConsole创建新控制台。 Then redirect cin / cout to that console as needed (depending on the requirements of your compiler's particular runtime implementation). 然后根据需要将cin / cout重定向到该控制台(取决于编译器特定的运行时实现的要求)。 Or, just use ReadConsole / WriteConsole instead. 或者,只需使用ReadConsole / WriteConsole

Yes, set the subsystem to WINDOWS and /ENTRY (entry point) to WinMainCRTStartup (or wWinMainCRTStartup if you're using unicode). 是的,将子系统设置为WINDOWS,将/ ENTRY(入口点)设置为WinMainCRTStartup (或wWinMainCRTStartup如果您使用的是Unicode)。 You can even set it only for the Debug configuration, so the console won't appear for the Release. 您甚至可以仅针对Debug配置进行设置,因此控制台不会针对Release出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM