简体   繁体   English

我可以在Win32 GUI应用程序中使用默认控制台,还是应该创建一个新控制台?

[英]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; 如果我首先使用FreeConsoleFreeConsole成功而不会出错; 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.

相关问题 如何从另一个C ++ win32控制台应用程序调用C ++ Win32 DLL - How Can I call a C++ Win32 DLL from another C++ win32 Console Application 我应该安装什么 package 以获得 Win32 控制台应用程序(Visual Stdio 2019) - What package should i install to get Win32 console application (Visual Stdio 2019) 如何从Win32 C ++应用程序输出到父控制台窗口? - How can I output to the parent console window from a Win32 C++ application? 如何将项目win32控制台应用程序转换为C ++ Windows窗体? - How can I convert project win32 console application into C++ Windows Form? 从Windows 10上的Win32 GUI应用程序输出到控制台 - Output to console from a Win32 GUI application on Windows 10 Win32 控制台应用程序 - Win32 Console Application 如何在.NET应用程序中使用从Win32 CryptoAPI生成的密钥Blob? - How can I use a key blob generated from Win32 CryptoAPI in my .NET application? 我可以在Win32 DLL中使用LoadLibrary吗? - Can I use LoadLibrary inside my Win32 DLL? 显示控制台和Win32 GUI - Display console and win32 GUI 是否可以在Windows 8 OS和Visual Studio 2012上通过GUI创建完整的Vista win32应用程序? - Can I create full fledged Vista win32 applications with GUI using Windows 8 OS with Visual Studio 2012?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM