简体   繁体   English

Win32应用程序,具有控制台输出,没有新窗口

[英]Win32 application with console output and without new window

I'd like to create a tool that can either act as command line (display some console output based on input parameters), or display a window, based on input parameters. 我想创建一个可以充当命令行(基于输入参数显示一些控制台输出)或基于输入参数显示窗口的工具。

I'm using MSV2012 with C++, and it seems you have to 'choose' between console and window app. 我正在将MSV2012与C ++一起使用,看来您必须在控制台和窗口应用程序之间“选择”。

I know the net is filled with samples which use AllocConsole() and redirect std::out, but it doesn't make it feel like a command line application : calling the exe from the windows console will open a new window with the console output... 我知道网络中充满了使用AllocConsole()并重定向std :: out的示例,但它并不像命令行应用程序:从Windows控制台调用exe会打开一个带有控制台输出的新窗口...

Is there a way to have it use the current console window instead of allocating a new one? 有没有办法让它使用当前的控制台窗口而不是分配一个新的窗口?

If not possible, I'll make 2 applications instead, but that's a pity.. 如果不可能的话,我将制作2个应用程序,但这很可惜。

Someone else may have a more authoritative answer, but I don't believe it's supported. 其他人可能会有更权威的答案,但我不认为它会得到支持。

The usual workaround is to create a Windows app, but have a command-line wrapper that launches it from the CLI (and provides a channel for communicating with the original console). 通常的解决方法是创建Windows应用程序,但有一个命令行包装程序,可从CLI启动它(并提供与原始控制台进行通信的通道)。

它在技术上不受支持,但是我找到了一个很好的解决方案,方法是获取当前进程的快照,查找父进程,如果是控制台应用程序则附加到其控制台或使用AllocConsole创建一个,重定向输出,获取父进程的线程如果它是cmd.exe并挂起它,则在我退出我的应用程序之前将其恢复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM