将消息返回到启动 MFC 应用程序的同一命令行

Getting the messages back to the same command line from where the MFC application was launched

我正在从命令行执行一个MFC 应用程序,它需要四个命令行arguments.One,参数是目录路径。 如果路径错误,那么我想在同一 命令行

上显示消息 "Bad Path"

注意:为了显示我不想使用新的命令行。

基本上不支持。有一种已知的 "workaround",使用 AttachConsole(-1) 附加父进程控制台。当然有缺点(例如,父控制台不会等待您的 EXE 终止,因为它不是 "console" 应用程序)。总之,基本思路:

void WriteToParentConsole()
{
    if (AttachConsole(-1))
    {
        char msg[] = "Bad Path!";
        WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, sizeof(msg), NULL, NULL);

        // send ENTER (optional)
        // ::SendMessage(GetConsoleWindow(), WM_CHAR, VK_RETURN, 0);

        FreeConsole();
    }
}

例如,您可以查看这篇文章,或者 google AttachConsole/GUI vs 控制台上的内容以获取更多信息: http://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/