如何从Windows GUI写入CommandPrompt? [英] How do I write to the CommandPrompt from Windows GUI?

查看:59
本文介绍了如何从Windows GUI写入CommandPrompt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作环境:Windows 7,Visual Studio 2010,CLR GUI。

Operating Environment: Windows 7, Visual Studio 2010, CLR GUI.

因此,我被赋予了一个不光彩的任务,即增强了一个从GUI启动的GUI应用程序。命令提示符。因为它是。因为以前的实施者的设计决策很差。无论如何,它会根据输入参数启动几个GUI之一。

So I've been given the unglorious task of enhancing a GUI application that is started from a command prompt. Because it is. Because poor design decisions by previous implementers. Anyway, it launches one of several GUIs depending upon the input arguments.

我希望能够(如果)在时返回到同一命令提示符窗口用户键入代码无法理解的内容。

I'd like to be able to print back to the same command prompt window if (when) the user types something that the code doesn't understand.

这是我尝试过的(都不输出任何内容):

Here's what I've tried (none of which output anything):

int main( array<System::String^>^ args )
{
  Application::EnableVisualStyles();
  Application::SetCompatibleTextRenderingDefault(false);

  OutputDebugString("hello");
  Trace::WriteLine("hello");
  Debug::Trace::WriteLine("hello");
  Console::WriteLine("hello");
  std::cout << "hello";
  printf("hello");
  return 0;
}

谢谢!

更新:我不想使用 AllocConsole(),因为这会打开一个新控制台,该控制台随所有数据一起消失当应用程序退出时。同样,弹出消息框也将不起作用。我正在寻找一种使输出持久化的方法。

Update: I don't want to use AllocConsole(), as that opens a new console that disappears along with all of the data when the application exits. Similarly, a pop-up message box won't work. I'm looking for a way to make the output persistent.

我迄今为止可以从应用程序获取输出的唯一方法是通过消息框(非持久性)或打开一个新的控制台,该控制台在应用程序退出时消失(通过 AllocConsole())。而且我是从命令提示符运行的,而不是调试器的播放按钮。

The only way I can get output from the application to date is via a message box (non-persistent) or opening a new console that disappears when the application exits (via AllocConsole() ). And I'm running from a command prompt, not the debugger's "Play" button.

更新
为什么要投票不做研究?我花了一天的时间来解决这个问题,浏览了数十条试图找到解决方案的帖子,到目前为止,我发现其他人也在寻找相同的答案,但没有找到答案。 AllocConsole()或更改项目类型始终是解决方案,但对我而言都不是解决方案。

Update Why the down-vote for not doing research? I spent a day trying to solve this, looking through dozens of posts trying to find a solution, and to date I've found others looking for the same answer, but not finding it. AllocConsole() or changing the project type is always the solution, but neither is a solution for me.

更新
我添加了完整代码,即2条语句。这就是所有代码。很简单。我跳过了GUI的启动,因为我现在不关心它,我只想将其打印回启动应用程序的控制台。最基本的HelloWorld。如果有我需要发布的项目设置,我不知道哪些设置是有意义的。在GUI开启之前,我想在这里打印到控制台。如果用户输入错误,则无法显示GUI。

Update I added the "full code", which is the 2 statements. THAT IS ALL THE CODE. So simple. I'm skipping the start of the GUI because I don't care about that right now, I just want it to print back to the console where the application was started. The most basic HelloWorld. If there are project settings I need to post, I don't know which ones would be relevant. This is where I want to print to the console, before the GUI is ever up. I can't show the GUI if there is an error in the user input.

推荐答案

右键单击项目,选择属性

在链接器->系统下,将子系统从Windows更改为控制台。

Right click on the project, select Properties
Under Linker -> System, Change Subsystem from Windows to Console.

Windows子系统应用程序无法写入控制台,而是将子系统更改为控制台(可以写到调用控制台),应用程序的窗体部分仍然可以工作(在Visual Studio 2010中测试)。

A Windows subsystem application cannot write to console, but by changing the subsystem to Console (which can write to the calling console), the Form part of the application can still work (tested in Visual Studio 2010).

这篇关于如何从Windows GUI写入CommandPrompt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆