DLL Main()

  • Thread starter Thread starter Rene
  • Start date Start date
R

Rene

When you build an exe application, you can count on that the "static void
Main()" function will bee the first one to be called when the application
starts up.

Is there something similar for dlls? Is there a "Main" entry point that is
called when the dll is first loaded?

Thanks.
 
What are you trying to do ?

In .NET you are working on classes. A shared constructor could perhaps help.
Or do you mean a "classic" DLL ? (which have a DLLMain function if I
remember) ?
 
No.

Class library's (DLLs) can not be started directly. They are only to
be referenced from an exe (WinForm/Console/etc).

Cheers,
Adam Cooper
 
When you build an exe application, you can count on that the "static void
Main()" function will bee the first one to be called when the application
starts up.

No you can't. Static constructors may run before Main for example.

Is there something similar for dlls?

..NET v2.0 supports having a "module initializer", but that feature
isn't available i C# since it doesn't support global methods. Usually
you can use a static constructor instead, like Patrice suggested.


Mattias
 
A shared constructor could perhaps help.
How do you specify a Shared Constructor.

I would like to accomplish something similar to what Mattias motioned on the
other post, something that works as a "module initializer". Would a Shared
Constructor work like that? Do I have to declare this constructor on every
class because I don't know which class will be access first?

Thanks.
 
Could you describe from a non technical point of view what you are after ?

I don't see for now why you would have to do that on each and every class
i.e. my first thought would be to put the functionalities that needs some
kind of initialization in its own class so that you'll guarantee that the
constructor for this class runs before other classes uses it.

For shared or static constructors see :
http://msdn.microsoft.com/library/d...y/en-us/csspec/html/vclrfcsharpspec_10_11.asp
(C#) or
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec7_2_2.asp

Are your using 1.1 or 2.0. Another response said that 2.0 might have
something that could be of some interest for you...
 
Rene said:
When you build an exe application, you can count on that the "static
void Main()" function will bee the first one to be called when the
application starts up.

Is there something similar for dlls? Is there a "Main" entry point
that is called when the dll is first loaded?

Your assembly can have a class called <Module> which represents the
module itself. Managed C++ allows you to create global methods, and
these are part of the <Module> class. ILDASM does not show this class
but Reflector does. Managed C++ (/clr or /clr:pure) will use the static
constructor of this class to perform once only module initialization.

You can do something similar if you write IL. C# and VB.NET treat
<Module> as an invalid name for a class. Although managed C++ will
create a <Module> class you cannot add such a class to your code (the
name is treated as being invalid).

Richard
 
I am not exactly sure what could be the use case where you require assembly level initialization.

Anyways, if you need this to be done in a single project, a dirty way of doing this will be to handle the AssemblyLoad event of the AppDomain and selectively invoking some initialization routine.

HTH, Metallikanz!
 
Metallikanz! said:
I am not exactly sure what could be the use case where you require
assembly level initialization.

I've wanted it a couple of times. For instance, if many plugins are
contained in an assembly, it would be nice to be able to make the
assembly itself register the plugins against the loader, rather than
make the loader find them all.
Anyways, if you need this to be done in a single project, a dirty way
of doing this will be to handle the AssemblyLoad event of the
AppDomain and selectively invoking some initialization routine.

Except you'd have to subscribe to the event *before* the assembly is
loaded...
 
Back
Top