如何知道我们是在一个控制台或一个窗口应用程序? [英] How to know whether we are in a console or a windowed app?

查看:142
本文介绍了如何知道我们是在一个控制台或一个窗口应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:编程ac / c ++ win32-mfc库

Context : programming a c/c++ win32-mfc library

如何知道我们是在一个控制台还是一个窗口应用程序?

How to know whether we are in a console or a windowed app?

推荐答案

您可以通过调用win32函数 GetConsoleWindow 。如果它返回NULL,则没有控制台连接到进程。然而,这不一定会告诉你,如果你是在一个窗口应用程序运行或不。例如,我可以有一个窗口应用程序使用 AllocConsole < a>在启动时为调试输出分配一个控制台,在这种情况下,您将同时拥有两个。我可以看到你所描述的另一个问题是一个应用程序可能没有窗口和没有控制台连接(例如一个windows服务)。

You can determine if there is a console currently attached to the process by calling the win32 function GetConsoleWindow. If it returns NULL then there is no console attached to the process. However this will not necessarily tell you if you are running in a windowed app or not. For example I could have a windowed app that uses AllocConsole on start-up to allocate a console for debug output in which case you would have both at the same time. The other problem I can see with what you describe is an application might have no windows and no console attached (A windows service for example).

我要有同意litb和马丁在这一个以及。如果你的库需要知道这个,那么它可能不是足够的解耦。如果你使用它来确定在哪里发送调试输出,例如最好的方法是使用cout或cerr,并让使用库的应用程序处理将流重定向到它想要的地方。

I'm going to have to agree with litb and Martin on this one as well. If your library needs to know this then it probably isn't decoupled enough. If you are using this to determine where to send debug output for example the best approach would be to use cout or cerr and let the application that is using the library deal with redirecting the stream to where it wants it.

这篇关于如何知道我们是在一个控制台或一个窗口应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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