[英]Can I use the default console in a Win32 GUI application, or should I create a new one?
I'm new to the Windows API and am programming in C++. 我是Windows API的新手,我正在用C ++编程。 I would like to have a console to output information to and receive keyboard commands via GetMessage. 我想有一个控制台,通过GetMessage输出信息并接收键盘命令。 However, I can't just create a console application because if I do, it's impossible to read keyboard messages sent to that console with GetMessage
. 但是,我不能只创建一个控制台应用程序,因为如果我这样做,就无法读取使用GetMessage
发送到该控制台的键盘消息。 Reacting to keyboard input via GetMessage is a requirement for this project. 通过GetMessage对键盘输入做出反应是该项目的必要条件。
When I create Win32 GUI application in Code::Blocks 13.12 (using MinGW to compile) and call AllocConsole
at the beginning, I get error 5: "Access is denied". 当我在Code :: Blocks 13.12中创建Win32 GUI应用程序(使用MinGW编译)并在开头调用AllocConsole
时,我收到错误5:“访问被拒绝”。 If I instead first use FreeConsole
, FreeConsole
succeeds without error; 如果我首先使用FreeConsole
, FreeConsole
成功而不会出错; if I then use AllocConsole
, a console window appears. 如果我然后使用AllocConsole
,则会出现一个控制台窗口。 The MSDN description of FreeConsole
is: FreeConsole
的MSDN描述是:
Detaches the calling process from its console. 从其控制台中分离调用进程。
This indicates that before I call FreeConsole, a console already existed (even though I couldn't see it and didn't explicitly create it). 这表明在我调用FreeConsole之前,已经存在一个控制台(即使我看不到它并且没有明确创建它)。 Is it an invisible console, or is it the one that always appears when running a Code::Blocks project? 它是一个不可见的控制台,还是在运行Code :: Blocks项目时总是出现的控制台? Is it pointless for me to use FreeConsole
and then AllocConsole
? 使用FreeConsole
然后使用AllocConsole
对我来说毫无意义吗? Is there instead a way to make the console that already exists visible (if it's invisible) and able to receive keyboard input via GetMessage
? 是否有一种方法可以使已经存在的控制台可见(如果它不可见)并且能够通过GetMessage
接收键盘输入?
Here's an example of the stripped-down code that exhibits this behavior: 以下是展示此行为的精简代码示例:
#include <windows.h>
DWORD dw = 0;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
if (FreeConsole() == 0) {
dw = GetLastError();
return dw;
}
if (AllocConsole() == 0) {
dw = GetLastError();
return dw;
}
return 1;
}
When I create Win32 GUI application in Code::Blocks (using MinGW to compile) and call
AllocConsole
at the beginning, I get error 5: Access is denied . 当我在Code :: Blocks中创建Win32 GUI应用程序(使用MinGW进行编译)并在开头调用AllocConsole
时,我收到错误5: 访问被拒绝 。
The explanation for your call to AllocConsole
failing is that you are actually building a console application. 您对AllocConsole
的调用失败的原因是您实际上正在构建一个控制台应用程序。 Even though you use WinMain
, mingw will still produce, by default, an executable that targets the console subsystem. 即使您使用WinMain
,默认情况下,mingw仍会生成一个针对控制台子系统的可执行文件。 You can use a tool like dumpbin
to inspect the PE header to confirm my conclusion. 您可以使用像dumpbin
这样的工具来检查PE头以确认我的结论。
Compile with -mwindows
to make sure that the executable targets the GUI subsystem. 使用-mwindows
进行编译以确保可执行文件以GUI子系统为目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.