Hemant
Hemant

Reputation: 19826

What makes an app console or Windows Form application?

[Visual Studio 2008]

I created a new project for console application and modified it to look like this:

class Program
{
    static void Main (string[] args) {
        Thread.Sleep (2000);
    }
}

Then I created another project for Windows Form application and modified it:


static class Program
{
    //[STAThread] commented this line
    static void Main (string[] args) { //Added args
        //Commented following lines
        //Application.EnableVisualStyles ();
        //Application.SetCompatibleTextRenderingDefault (false);
        //Application.Run (new Form1 ()); commented this line
        Thread.Sleep (2000);
    }
}

Now I have neither written Console functions (Console.Write etc.) in first application nor I have written forms related operations in second one. Looks identical to me.

Still first application shows BLACK window and second one doesn't show anything. What makes it work like this?

Upvotes: 19

Views: 12444

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1062770

Under the bonnet, there is no difference in a winform vs console exe except for a flag in the PE-header that says "I need a console". The PE header is not controlled from your C# (since it is a compile thing, not a runtime thing), so this is defined in the project file instead (<OutputType>...</OutputType>).

Or at the command-line (csc /target:exe vs csc /target:winexe).

Arguably, they could have used an assembly-level attribute that the compiler intercepted - but would that really have helped? Probably not.

Upvotes: 9

AlexDrenea
AlexDrenea

Reputation: 8039

In project properties, Application Tab, Output Type you can set to 'Windows Application' or 'Console Application'.

I believe that behind the scenes VS does exactly what Fredrik presented in his post.

Also, setting it to Console Application will show you the black console application for the windows Forms project.

Upvotes: 9

jerryjvl
jerryjvl

Reputation: 20121

If you look in the project file (csproj) you'll see that the target is defined there as either a console or windows app.

Upvotes: 3

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158309

If you inspect the exe files usine ILDASM you can see that there is a difference in the Manifest (look for "subsystem").

In a Winforms application:

.subsystem 0x0002       // WINDOWS_GUI

In a console application:

.subsystem 0x0003       // WINDOWS_CUI

There may be more differencies in the IL code.

When it comes to what makes the compiler emit this differently in the two cases, this is controlled by the project file's OutputType value:

In a Winforms application:

<OutputType>WinExe</OutputType>

In a console application:

<OutputType>Exe</OutputType>

Out of curiosity I also checked that value for a Class Library project:

<OutputType>Library</OutputType>

Upvotes: 34

Related Questions