Is there a way for a windows-subsystem application to offer optional console output?

余生长醉 提交于 2019-12-24 12:42:20

问题


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.

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

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

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.


回答1:


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

AttachConsole(ATTACH_PARENT_PROCESS);

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 :)



来源:https://stackoverflow.com/questions/19168763/is-there-a-way-for-a-windows-subsystem-application-to-offer-optional-console-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!