简体   繁体   中英

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?

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>

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.

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.

如果您查看项目文件(csproj),您将看到目标在那里被定义为控制台或Windows应用程序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM