Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 477
moala Avatar asked Jan 15 '09 16:01

moala


1 Answers

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

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.

like image 160
Kevin Loney Avatar answered Sep 19 '22 13:09

Kevin Loney