这是我在《Programming Microsoft Windows with C#》(此书作者是大名鼎鼎的Charles Petzold)一书中找到的描述:
This compiler switch doesn't do anything very profound. It really only sets a flag in the executable file that indicates how the program is to be loaded an run. If an executable is flagged as a Console Application and is started from Windows, the Windows operating system creates a Command Prompt window that launches the program and displays any console output from the program. If the console application is started from within the Command Prompt window, the MS-DOS promptdoesn't return until the program terminateds. If the executable is flagged as a Windows Application, no Command Prompt window is created. Any console output from the program goes into the bit bucket. If you start such a program from the Command Prompt window, the MS-DOS prompt appears again right after the program is launched. The point is this: nothing bad happens if you compile a Windows Forms application as a console application!
作者之前提到的编译选项是/target:exe和/target:winexe,前者生成控制台程序,后者生成Windows程序,这是C#的编译器,而VC++的连接器的选项却有些不同,看这张图:
如果你在应用程序向导里指定的是一个Windows程序,而你后来却在这个连接器选项里选择Console,那会怎么样呢?——会连接失败!因为VC++连接器认为Console程序和Windows程序的入口函数是不同,这样简单的一改它会找不到入口函数,所以连接失败。
但不管这样,通过Charles Petzold的这段描述,我们对Console程序和Windows程序的认识应该是没什么问题了,两者其实并没有什么根本不同,只是Windows根据PE文件中的标识,用稍微不同的方法来运行这两种程序而已。
BTW:Windows程序可以创建自己的控制台(参考AllocConsole等API),控制台程序也可以创建窗口。