TechAurelian
TechAurelian

Reputation: 5821

Big C# source file with Windows API method signatures, structures, constants: will they all be included in the final .exe?

I want to put all the signatures of Windows API functions I'm using in programs in one class, such as WinAPI, and in a file WinAPI.cs I will include in my projects. The class will be internal static and the methods public static extern. (Something like the huge NativeMethods.cs from the .NET Framework source code).

Let's say WinAPI.cs has one hundred native methods signatures like:

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  1. If I'm only going to use a few of them in a project (even one or two), if I include the WinAPI.cs file in my project, will the final .exe be filled with the unnecessary method declarations I'm not using (the other 99 or so)?

  2. What about native structure declarations, If I'm not using them in the current project, will they still be included in the final .exe?

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
        }
    
  3. What about constants declarations or readonly members like:

    public const int WH_JOURNALPLAYBACK = 1;
    public static readonly int WM_GETTEXT = 0x0D;
    

Upvotes: 8

Views: 620

Answers (4)

Hans Passant
Hans Passant

Reputation: 942518

Don't sweat the small stuff here. These are just declarations, there is no IL for them in the final assembly. They just take up some space in the metadata. Odds are very high that they don't even get loaded from the assembly, especially when you keep the declarations together. Metadata is only ever loaded into RAM when it is used, 4096 bytes at a time.

Btw, you explicitly don't want to use readonly for these constant declarations. Now you do get them to take space, even when they are not used. One of the few cases where a public const is a good idea. These are manifest constants, their value never changes. Like Math.PI.

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 416159

All of this will be included. However, it will be included as IL and when the program runs it's just compiled machine code. We're talking maybe 40K of space at most.

Upvotes: 2

MichaelMocko
MichaelMocko

Reputation: 6066

All elements which you have stated will be included in final assembly. The only way to get rid of them from final exe file is precompilation directives usage. For example:

#if EXCLUDE_METHODS

//unused methods here

#endif

Upvotes: 2

ILya
ILya

Reputation: 2788

If I'm only going to use a few of them in a project (even one or two), if I include the WinAPI.cs file in my project, will the final .exe be filled with the unnecessary method declarations I'm not using (the other 99 or so)?

Yes, all code in your project will be compiled into your assembly;

What about native structure declarations, If I'm not using them in the current project, will they still be included in the final .exe?

Yes. They will be included too.

   What about constants declarations or readonly members like:

Yes. They will be included too.

Upvotes: 1

Related Questions