tags:

views:

2792

answers:

6

What differences are there between Windows and Console applications ?

When creating a new project in Visual C++ , it asks to choose either of the above .

A: 

Console application runs from a windows command line (start / run / cmd)

A Window application is preset so you can program a GUI application that runs within the Windows environment.

Ólafur Waage
+15  A: 

The sole difference is that a console application always spawns a console if it isn't started from one (or the console is actively suppressed on startup). A windows application, on the other hand, does not spawn a console. It can still attach to an existant console or create a new one using AllocConsole.

This makes Windows applications better suited for GUI applications or background applications because you usually don't want to have a terminal window created for those.

On a more technical note, the only difference between a Console and a Windows executable is one byte in the PE header of the exe file. Toggling this byte manually (e.g. using a hex editor) converts the application type. This is a well-published hack that is used to create console applications in VB6 (where this type of application was not explicitly supported).

To determine and change the subsystem type of an application, you need to read parts of the PE header. The address of the subsystem data is not fixed though, because it's part of the optional file header whose position is determined by an address stored in the DOS file header (in the member e_lfanew). This address actually points to the _IMAGE_NT_HEADERS record which, in turn, includes the IMAGE_OPTIONAL_HEADER32 structure. This has an int161) member called Subsystem. The member's value is 2 for a Windows application and 3 for a console application. Other subsystems exist (in particular, POSIX and kernel). I've written a small VB6 application to change the subsystem of an application, which can be downloaded from ActiveVB as source code.

The PE format isn't very well documented but this document may serve as an introduction: Peering Inside the PE: A Tour of the Win32 Portable Executable File Format.


1) This doesn't really contradict my claim that only one byte differs: the most significant byte of this member is always 0. Only the least significant byte changes.

Konrad Rudolph
Thanks Konrad - great :-)
Rakesh Agarwal
@Rakesh, you should accept his solution if you like it, its the little V next to the answer.
Ólafur Waage
@Olafur , could you please tell me what is the significance of value '4' right above the little V next to the answer ?
Rakesh Agarwal
that's the number of "Up votes" -- 4 people (5 now) have voted in favor of this answer.
Leon Bambrick
what is that '2' beside the question asked ?
Rakesh Agarwal
Aren't there differences in referenced DLLs too?
David Schmitt
@Rakesh - No of upvotes for your question
Russ Cam
@SoapBox: see updated answer. The address isn't fixed, it has to be calculated. I've posted a link to some VB6 code to read this data. The MSDN article also shows how to do this.
Konrad Rudolph
+4  A: 

Besides the difference mentioned by Konrad, console and Windows applications behave differently when called interactively from the command prompt:

When you start a console application, the command prompt doesn't return until the console application exits. When you start a windows application, the command returns immediately.

This is not true for batch files; they will always wait until the application exits. (You can always use the start command to start an application without waiting.)

oefe
A: 

The difference is in the way the apps are stubbed out. When you use the console template, you have a stub that will fire up in a console. If you are already running in a console, it ignores the call to spin one up.

By the same token, a windows app is designed with a default form. If you want to clear it out, you can create a formless Windows Forms application that is essentially a console application without a console window.

As far as the guts of the app goes, they are essentially the same. The major difference is added on at the compile stage.

Gregory A Beamer
A: 

Console application: A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with some Graphical User Interface (GUI) operating systems, such as the Win32 console in Microsoft Windows. A user typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device. Many console applications such as command line interpreters are command line tools, but numerous Text User Interface (TUI) programs also exist. As the speed and ease-of-use of GUI applications have improved over time, the use of console applications has greatly diminished, but not disappeared. Some users simply prefer console based applications, while some organizations still rely on existing console applications to handle key data processing tasks.

Console based applications:

The following list contains a small selection console based applications:

Lynx — web browser irssi — irc client Mutt — e-mail client Midnight Commander — file manager Nano — text editor

Windows Forms:

Windows Forms is the name given to the graphical user interface application programming interface (API) included as a part of Microsoft's .NET Framework, providing access to the native Microsoft Windows interface elements by wrapping the existing Windows API in managed code. While it is seen as a replacement for the earlier and more complex C++ based Microsoft Foundation Class Library, it does not offer a paradigm comparable to model-view-controller.

The following is a simple C# program using Windows Forms.

using System; using System.Windows.Forms;

public class HelloWorld { public static void Main() { MessageBox.Show("Hello world!"); } }

A: 

Message Loop is also one of the difference:

http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows

Signcodeindie