Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    
like image 391
Alex Vang Avatar asked Nov 30 '10 14:11

Alex Vang


1 Answers

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.

like image 167
Hans Passant Avatar answered Sep 23 '22 18:09

Hans Passant