将基于控制台和基于Windows的应用程序结合起来
问题描述:
尽管我在我的应用程序中使用了DirectX以及通常的WinMain
函数,但如果应用程序是以特定参数启动的,那么我想创建输出到控制台(通常为std::cout
)。让我们将事情简单化:如果用户使用--help
参数调用应用程序,则应该显示一些帮助(使用boost::program_options
);否则,一切应该通常通过创建一个窗口等工作将基于控制台和基于Windows的应用程序结合起来
即使在我的Windows应用程序中,我怎么能写输出到控制台(如果应用程序被调用通过一个)?
背景资料:总的想法是,我运行游戏引擎之前,我将能够运行一些工具(无论是外部的,或纳入引擎),并得到它们的输出。
当前方法。我现在有两个单独的应用程序,一个启动程序和引擎,但是我希望尽可能合并它们。
答
如果您只需要为WinMain GUI应用程序创建控制台窗口,则需要调用AllocConsole函数。每个流程只限制一个。
例用C ...
#include <stdio.h>
WinMain(...) {
// parse the command line and check if --help is given
AllocConsole(); // allocates console window for your process
freopen("CON", "w", stdout); // redirects output to console
printf(...); // test output to the console window
FreeConsole(); // detaches your process from the console window
// continue here
}
这只会创建点播控制台窗口,如果你需要使用像printf
功能从GUI应用程序中显示的东西。它不会让你的应用程序同时具有控制台和GUI子系统。你需要两个.exe,所以你现在的方法是正确的。