views:

59

answers:

2
+3  Q: 

Windows console

Hello. Well, I have a simple question, at least I hope its simple. I was interested in win32 console for a while. Our teacher told us, that windows console is just for DOS and real mode emulation purposes. Well, I know it is not true, becouse DOS applications are runned by emulator which only uses console to display output. Another thing I learned is that console is built into Windows since NT. Well. But what I could not find is, how actually are console programs written to use console. I use Visual C++ for programming (well, for learning). So, the only thing I need to do for using console is select console project. I first thought that windows decides wheather it run app in console or tries to run app in window mode. So I created win32 program and tried printf(). Well, I could not compile it. I know that by definition printf() prints text or variables to stdout. I also found that stdout is the console interface for output. But, I could not find what actually stdout is.

So, basicly what I want to ask is, where is the difference between console app and win32 app. I thought that windows starts console when it gets command from "console-family" functions. But obvisously it does not, so there must be some code that actually commands windows to create console interface.

And the second question is, when the console is created, how does windows recognize which console terminal is used for what app? I mean, what actually is stdout? Is it a area in memory , or some windows routine that is called? Thanks.

+1  A: 

When you link a Win32 application you select whether it is Windows or Console. In the console case a console window will be allocated automatically (or, if the parent process has one, reused).

However a Windows (i.e. GUI) application can also show a console Window, just call the AllocConsole API.

You test application might fail to compile for a number of reasons, the error message should tell you whether you need to:

  • include "stdio.h"
  • reference the CRT (C Run Time) library
  • something else.

Normally if you select the console application in the new project wizard for a Win32 project these things should just work, otherwise you do need to select the right compilation and link options.

Richard
Thank you, but I was more interested in how actually is linking to console done. What part of code, not source, but code ater compilation says to Windows that I want to allocate console? Thanks.
B.Gen.Jack.O.Neill
A: 

To answer your second question, stdout in Windows is mapped to the HANDLE returned by GetStdHandle(STD_OUTPUT_HANDLE) which is by default mapped to CONOUT$. You can access that by calling CreateFile("CONOUT$", ...) if you are so inclined.

MSN