In the MSDN ".Net Framework Developer's Guide" it says that "...each
assembly can have only one entry point (that is, DllMain, WinMain, or
Main..."
So we've got "must" vs. "can" and "DLLMain" vs. "DllMain."
In playing with this, it doesn't appear to make any difference whether it's
included or not, or how it's spelled. It also never seems to get called
automatically upon loading. Since there aren't any variables living outside
of class scope in C#, it further seems that there wouldn't be much call (eh,
sorry) for DllMain anyway.
Can someone enlighten me as to the purpose and usage of DllMain in a pure
managed-code class library assembly? Is it possible to get it called
automatically when the assembly is loaded if one had reason to need such a
feature?
Thanks.
Michael Roper
> In Jesse Liberty's "Programming in C#" he says that "...an assembly must
> have exactly one entry point: DLLMain, WinMain, or Main. DLLMain is the
> entry point for DLLs..."
>
> In the MSDN ".Net Framework Developer's Guide" it says that "...each
> assembly can have only one entry point (that is, DllMain, WinMain, or
> Main..."
>
> So we've got "must" vs. "can" and "DLLMain" vs. "DllMain."
>
> In playing with this, it doesn't appear to make any difference whether
it's
> included or not, or how it's spelled. It also never seems to get called
> automatically upon loading. Since there aren't any variables living
outside
> of class scope in C#, it further seems that there wouldn't be much call
(eh,
> sorry) for DllMain anyway.
The only entry point you really need is Main(). In C# this method is already
part of the application, in dll's it's not needed. In VB.NET, a sub "Main"
doesn't seem to exist but is created in IL-Code automatically. In managed
code dlls you don't need to specify an entry point.
Regards,
--
Frank Eller [.NET MVP]
www.frankeller.de
.NET Developers Group Munich- www.munichdot.net
Chris
3rd edition, (c) 2003. "Updated for .NET 1.1 & Visual Studio .NET 2003"
> Is that quote from the MSDN a current one?
> I suspect this is all based on a beta version of the framework.
Doesn't seem so.
So, is the answer to my question then, that DllMain is vestigial and has no
purpose in a managed class library? Is there ever a need to perform
initialization work when a library is loaded, and if so, how is that done in
the absence of DllMain?
Thanks again.
Michael Roper
Don't know. I know that windows DLLs have a DLLMain (not sure about the
spelling) and that's called to initialize global variables when the library
is loaded using LoadLibrary or by other methods, but I'm not sure that any
of that is necessary in .NET. Perhaps if you're doing some advanced P/Invoke
in a .NET dll you'd need to do some of this, but I don't see why. It may be
more of a method to support a couple of necessary hacks here and there in
the framework itself to get a nice tight integration with the GUI or other
native code.
Chris