How do we know if we are in a console or windowed application?

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

How do we know if we are in a console or windowed application?

+7
c ++ windows winapi console
source share
2 answers

You can determine if there is currently a console that calls the win32 function GetConsoleWindow . If it returns NULL, then the console is not connected to the process. However, this does not necessarily tell you whether you are working in a windowed application or not. For example, I could have a windowed application that uses AllocConsole at startup to select the console for debugging output, in which case you would have both at the same time. Another problem that I see with what you described is that the application may not have windows and the console is not connected (for example, the Windows service).

I, too, will have to agree with him and Martin on this. If your library needs to know this, then it is probably not sufficiently untied. If you use this to determine where to send debug output, for example, the best approach would be to use cout or cerr, and let the application using the library deal with redirecting the stream to where it wants it.

+13
source share

I'm not sure about this (without trying it myself), but you can call the GetStartupInfo function, which completes the STARTUPINFO structure.

Perhaps console and Windows applications set the STARTF_USESHOWWINDOW flag of the dwFlags member differently, and you can distinguish between them like this.

As mentioned in the comments on your question, this is usually not a good sign when the library needs to know such things - the more untied, the better.

Hope this helps.

+2
source share

All Articles