Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Good time of day to anyone!
While learning object oriented programming in university we got a task that requires creating dll so that some part of code is executed exactly on load,I mean something like DllMain in c++. I tried to surf the internet but managed to find only an advice to create static constructor,that will initialize all we need on first creation of object,BUT it dosen't, as the main requirement is that NOTHING can be rewritten in the original code,only line "using DllName" is allowed. So the question is: Is there any possibility to make Dll execute something exactly on it's load into project?
Thank you very much,hope someone will help me.
Posted
Comments
BillWoodruff 16-Oct-11 16:21pm    
Is this a WinForms project ? I believe you cannot do what you describe here ... unless your description has left something out.
Manfred Rudolf Bihy 17-Oct-11 5:16am    
Please unaccept my solution and accept those of BillWoodruff and SAKryukov.

Thanks for your cooperation!
BillWoodruff 23-Oct-11 7:21am    
Hi AvelN, I noticed someone down-voted my answer, and that is really unimportant to me, but in considering my words, I just wanted to make clear to you that in using the term "trick question" I did not mean to imply that this question was not a genuine, sincere, and interesting question ! With hindsight, I would have been more considerate in my reply if I had used the terms "tricky question." best, Bill

... edited to remove the possibly negative sense of my original response using the phrase "trick question" ...

Even though I think the question posed here is a "tricky" question that has no clear solution ... at least not in .NET WinForms, I feel compelled to clarify my earlier comment with the goal of avoiding creating any unnecessary confusion, and, possibly, clearing away a strange cloud of metaphysical smoke that has materialized here, perhaps smoke from an ancient yogi's campfire, a practitioner of Advaita Vedanta's 'neti neti' school, as espoused in the Avadhuta Gita :)

While it is true, as MSDN says: "The user has no control on when the static constructor is executed in the program," you, the programmer, can force the constructor of a static class to be called.

For example, suppose I create a .NET project of type 'Class Library' which has one static class:
C#
using System;

namespace AStaticClassExperiment
{
    public class StaticClass
    {
        // the static constructor
        static StaticClass()
        {
            Console.WriteLine("myField = " + myField);
        }

        // a public static Property of type string
        public static string myField { get; set; }
    }
}
Now I compile this into a DLL. And, then, I create a new .NET WinForm project in which I add a reference to that DLL:
using AStaticClassExperiment;
If, in the code for this new WinForm project, I execute this code:
AStaticClassExperiment.StaticClass.myField = "Hello from the DLL.";
The act of accessing a field of the static class guarantees the static constructor of the class will be invoked, as you can verify by examining the Output Window.

Can a single static class constitute the entire contents of a DLL: yes !

So, when a static class is referenced in a WinForms project, before some part of it is used/referenced: where is it ? what is it ? Well, you might think of it as an 'unused template,' or 'blueprint,' or 'to-do list:' that, once 'picked up' may well trigger a number of behaviors and alter its own internal state.

Hope this is helpful !
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 17-Oct-11 5:09am    
Thanks for pointing out the mistake I made. I took the final question of OP out of the context of what OP said previously: So the question is: Is there any possibility to make Dll execute something exactly on it's load into project?.

Cheers and have a 5!
Sergey Alexandrovich Kryukov 17-Oct-11 11:07am    
Thank you very much, Manfred.
--SA
Manfred Rudolf Bihy 17-Oct-11 5:19am    
I've addressed OP to accept yours and SA's solution instead!
Amir Mahfoozi 23-Oct-11 7:55am    
+5
I don't think .NET allows exactly what you need; and I think there is a good reason for this.

[EDIT] I would rather say it only seems to you that you need it, but in fact, with .NET, you don't need such thing. [END EDIT]

Essentially, in .NET there is no such thing as DLL. You can create them, but the concept of DLL is meaningless. Instead, meaningful entities are assemblies and executable modules of assemblies (many people don't know the difference just because VS allows creation of just one executable module per assembly, but raw C# compilers allow creation of separate executable modules which are non-assemblies). DLL is just one of possible extensions which does not carry any special meaning to assembly. In particular, there is no distinct difference between DLL and EXE (it does have entry point, but that's it); and EXE files (with entry point) could also be used as regular class-library assemblies — referenced or loaded during run-time of some .NET process.

With this shift in ideology of modularity, it looks natural that the boundaries between executable module are made much more transparent. During development, a developer does not really feel the boundary between parts of codes put in different assemblies in development, intellisense and debugging. In particular, the moment of the loading of assembly is made transparent (excluding the cases of loading assembly during runtime with the use of Reflection).
[EDIT] As Bill correctly noted in his comment to the answer by Manfred, "The user has no control on when the static constructor is executed in the program." [END EDIT]

Another essential factor is JIT (Just-in-Time compilation) used in .NET. Essentially, nothing is even compiled to native code until it needs to be used for the first time. This important factors renders execution on loaded making no sense.

In particular, the static constructors are not called at the moment of loading of assemblies, and not immediately after that. They are called when a class is to be used for the very first time in the Application Domain. Exact moment of this event is undefined. It only guarantees that a constructor is called before the class is used.

—SA
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 17-Oct-11 5:08am    
Thanks for pointing out the mistake I made. I took the final question of OP out of the context of what OP said previously: "So the question is: Is there any possibility to make Dll execute something exactly on it's load into project?".

Cheers and have a 5!
Sergey Alexandrovich Kryukov 17-Oct-11 11:08am    
Thank you very much, Manfred.
--SA
Manfred Rudolf Bihy 17-Oct-11 5:18am    
I've addressed OP to accept yours and Bills solution instead!
[Update]
As Bill and SA pointed out my solution is not exactly what OP layed out in the complete question. My solution only addresses what OP asked in his last question statement.
[/Update]

As far as I know are static fields initialized when a class is loaded. So you could initialize a private static field by calling a static method. I have not tried this, but I think it should work.

Regards,

MRB
 
Share this answer
 
v3
Comments
Espen Harlinn 16-Oct-11 16:33pm    
5+ :)
Sergey Alexandrovich Kryukov 16-Oct-11 21:28pm    
Well, there are some problems here. Please see my answer where I try to explain it.
--SA
BillWoodruff 16-Oct-11 16:54pm    
When a Class is loaded, yes, but then there has to be some invocation/operation/instantion, etc., on the Class to trigger the loading. In this case, if you accept the OP's statement as is, you can see he's saying he can do nothing but add a 'Using statement.

Static classes, even with a static constructor, behave the same way. And, as the MSDN docs say: "The user has no control on when the static constructor is executed in the program."

Trying code before answering is a wise choice :)
Sergey Alexandrovich Kryukov 16-Oct-11 21:26pm    
You are right, Bill. This is actually related to different ideology of loading in .NET, concept of assembly and JIT.
I tried to explain it in my solution, please see.
--SA
Sergey Alexandrovich Kryukov 16-Oct-11 21:32pm    
So I referenced this citation in my solution and credited your comment.
Thank you.
--SA
If you write using dllname in your code, it is early binding to DLL and onload of your application is occurred after all DLL were loaded.

If you need more control on loading DLLs you need to late bind to DLLs, so read this :
Late binding on native DLLs with C#[^]
Or this one :
http://dranaxum.wordpress.com/2008/02/25/dynamic-load-net-dll-files-creating-a-plug-in-system-c/[^]


And if you persist on "using dll" and know when exactly DLL loads, As I know it is only possible by debugging API when loading an external process or attaching to another running process which are not what you mentioned here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900