Home > Back-end >  Is it possible to have the program entry point in a library assembly?
Is it possible to have the program entry point in a library assembly?

Time:07-25

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.

CodePudding user response:

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.

  • Related