有一种方法为Windows子系统应用程序提供可选控制台输出? [英] Is there a way for a windows-subsystem application to offer optional console output?

查看:352
本文介绍了有一种方法为Windows子系统应用程序提供可选控制台输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序应用程序,通常仅限于GUI,并声明WINDOWS子系统。它不会在启动时打开控制台窗口。

I have a Windows application application that is normally GUI-only and declares the WINDOWS subsystem. It doesn't open a console window when launched.

唉,当应用程序恰好从控制台窗口启动时,我想提供额外的控制台输出交互式cmd.exe)。

Alas, I would like to offer additional console output when the application happens to be started from console window (say, from interactive cmd.exe).

有没有办法检测是否有一个进程up the chain有一个打开的控制台,并附加到该控制台?

Is there a way of detecting if some process "up the chain" has an open console, and to attach to that console?

我找到的最近的是 a方法显式打开一个gui应用程序的控制台,但是如果没有已经存在的控制台,我不想打开控制台。

The closest I've found is a way to explicitly open a console for a gui application, but I don't want to open a console if there isn't one already there.

推荐答案

乍一看,它似乎像 AttachConsole() 函数将允许您附加到父进程的控制台:

At first glance it seems like the AttachConsole() function will let you attach to the console of your parent process:

AttachConsole(ATTACH_PARENT_PROCESS);

如果进程实际上没有控制台,则函数将失败, ERROR_INVALID_HANDLE 。如果您的父进程不再存在,该函数也将失败。如果失败,您可以调用 AllocConsole() 创建自己的。

If the process doesn't actually have a console the function will fail with ERROR_INVALID_HANDLE. The function will also fail if your parent process no longer exists. If it fails you can then call AllocConsole() to create your own.

我从来没有尝试过,所以实际上不知道它是否可以工作)。

I've never tried this so don't actually know if it will work or not :)

这篇关于有一种方法为Windows子系统应用程序提供可选控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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