1

In compiled system languages (like C/C++), generally, the entry point is resolved at link time, which gives the ability to have the main function in a DLL so the linker won't complain and set the entry point address to the symbol in the DLL (or the function in the import library, not sure about that).

I recently started using C# and I would like to do something similar, where I have the Main method in a library (which preferably build against .NET Standard) and the actual exe don't define any entry point and uses the one in the library.

I get that I could just write a Main method in the exe and call the Main of the library in it, but the point is that I would like to avoid that.

I believe Winforms and WPF provide something similar, so hopefully what I'm trying to do is possible, otherwise, please educate me on the reasons why .NET doesn't provide such mechanism.

ctinarelli
  • 45
  • 1
  • 6
  • Perhaps [this](https://stackoverflow.com/a/9577595/5509738) might work for you or [ModuleInitializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-6.0) – Karen Payne Jul 23 '22 at 18:18
  • @KarenPayne I tried your first option but it just don't work. I can't specify in the executable project that I want to use the `Main` in the library, and if I don't specify any the compiler give me an error telling that the app don't contain an entry point. Using `ModuleInitializer`, would I be able to set the entrypoint at runtime ? Like something related to `App_Domain` ? – ctinarelli Jul 23 '22 at 19:49
  • `would I be able to set the entrypoint at runtime` No – Karen Payne Jul 23 '22 at 20:07
  • @KarenPayne I meant, setting what method will be called first instead of the actual entry point of the program, since `ModuleInitializer` seems to let you execute code before the specified entry point. – ctinarelli Jul 23 '22 at 20:27

1 Answers1

2

There seems to be a misunderstanding here:

In .NET, the only difference between a DLL and an EXE is purely the presence of .entrypoint in the header, which identifies the starting method that the CLR bootstrapper calls.

If there isn't one then it's a library rather than an application. It makes no sense to have an EXE which has no .entrypoint on any method, it won't run because the CLR bootstrapper won't know what to do with it.

Winforms and WPF do not do what you claim: they have a normal .entrypoint, it just happens to be boiler plate code which you don't need to worry about.

For clarity: the actual extension of the file is somewhat irrelevant. It's perfectly legal to call an assembly whatever you want, and you can link an assembly containing .entrypoint within another one, whether or not it has .exe extension or any other.

However, Windows will only bootstrap a file automatically if it has the .exe extension, as the CLR bootstrapper itself needs bootstrapping. You can though call AppDomain.ExecuteAssembly which essentially does the same thing from within another app.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • Thanks for your explanation. Then, if I understand correctly, it is not possible to specify an assembly `.entrypoint` to point to a method in another assembly , as it have to be local to the building assembly; what I'm trying to do is basically impossible. – ctinarelli Jul 25 '22 at 07:30
  • 1
    Correct. It is however theoretically possible to split a single assembly into separate modules (files) however they function as a single assembly and must be compiled together. I'm not sure how that behaves if the `.exe` does not have the `.entrypoint`, but difficult to test as I don't think Visual Studio can compile that way. You could try it with ILAsm. See also ECMA-335 specification, section I.15.4.1.2 – Charlieface Jul 25 '22 at 08:04