Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!

i have a nice trouble with DLL (Dynamic linking library).
As i know, when an *.exe is loaded, the OS will check the DLL file of program.
If DLL was found => check continue :-)

If the code of DLL which need by program had been loaded into memory => skip
Else => load the next code in DLL into memory => continue.

More knowledge, before running, the exe file has to be compiled by compiler to Assembly code.
That mean, "Compiler is a tool to open exe file". So when loading, Compiler opened and "bring" a segment code (part of DLL code) to OS. Then OS => load DLL file => write to memory.

My trouble is:
Is my understand true?
The code which received by OS was "public static void Main()" or "1111010010101"?
And... Can i write code in order to "open" and "bring" the code like a compiler by C# (not design new compiler, it's too complex)?

// p/s: i had downloaded GCC compiler source code. But i didn't know what is the code i need ^^!

Please!
Thank you very much!
Posted
Updated 26-Jul-14 8:46am
v2
Comments
Andreas Gieriet 26-Jul-14 15:34pm    
Tell us about your background to know where to start to explain. You seem to confuse several terms. You talk about C#, then about GCC, than about writing your own compiler and then again about main versus machine code (I guess)...
Cheers
Andi

"the exe file has to be compiled by compiler to Assembly code"

No.

"Compiler is a tool to open exe file"

No.


The closest to that is that, when using a DLL, the compiler needs to access the publicly available symbols of the DLL. But that's at compile time, not at run time.


I hope someone else responds with a link to a good explanation.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jul-14 17:55pm    
5ed. As to "someone else responds...", I tried it, but had to start with following your approach, to add some more "No" lines. :-)
—SA
First of all, let me congratulate you with having a "nice trouble". No doubt, having an ugly trouble would be much worse.

Let me continue with Solution 1.

"…when an *.exe is loaded, the OS will check the DLL file of program".

No. The statement makes no sense, because there is no such thing as "DLL file of the program".

"Compiler is a tool to open exe file".

No. Compiler is a tool to compile source code into object code (whatever it is).

"Compiler opened and "bring" a segment code…".

No. Compiler does not do it. It reads source code and just creates other file(s), the result of compilation.

"Is my understand true?"

No, apparently not.

Can I write code in order to "open" and "bring" the code like a compiler by C#…"

No. By two reasons: 1) a compiler does not do it; 2) your understanding of everything you mention is wrong.

Finally, let's get back to your main question: "How DLL works".

To really understand it, my answer won't be enough, because this is not what you need to learn first. First, you need to learn how… everything else work: EXE, application, process, segment, memory, CPU, compiler, linker, loader… you name it. You seem to be too much confused. I can only give you few ideas.

First of all, DLL, EXE file is majorly the same thing. "DLL" and "EXE", as part of file names, is just the naming convention, there are many more "extensions" like that, they all cover one common thing: PE file:
http://en.wikipedia.org/wiki/Portable_Executable[^].

The structure of the file is pretty complex, you can learn it if your read appropriate literature. They all often called just "executable files", including DLL. All such files contain executable code, and not only the native code for some CPU instruction-set architecture. The PE modules built as .NET assemblies (or separate modules used by assemblies, which is also possible but rarely used) contain IL code which is compiled to native code piece by piece during runtime (on per-method basis, normally).

The major difference Dynamic Link Library make is that they may or may not have entry point (and the entry point they may have has different signature and different purpose than EXE). This is a kind of a convention known by a system loaded. This piece of the OS takes the file name from the Shell or OS call and tries to load the file and put it to execution. The entry point is where the execution starts. The OS passes command line parameters and environment structure to the process and starts it with the defined entry point. So, when OS loads an EXE, a new process is supposed to be created.

As to the DLLs, they are referenced by other PE modules. There are certain rules for funding actual file by their names (and .NET has a special mechanism called strong naming which practically can make the assemblies world-unique; this is not always used). All referenced DLLs should be loaded at once before the process starts; if one is absent, the error diagnostics will be shown even before the process starts.

The DLL can also be loaded later, during runtime. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683152(v=vs.85).aspx[^] (see other related API functions on these pages).

Compare with: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx[^].

Those rules are only applied to standard OS way to load things and start processes. First of all, EXE files can be used as DLLs, for loading in a running process and using some exported code from them. And you can develop your own runtime system, where some host process (perhaps originally loaded and started in a "standard" way) loads some DLLs build with its special API, to load then and use as "executable modules". Something similar is really done in different plug-in architectures.

See also:
http://en.wikipedia.org/wiki/Main_function[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559%28v=vs.85%29.aspx[^].

Compare with: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx[^].

—SA
 
Share this answer
 
Comments
Andreas Gieriet 26-Jul-14 18:54pm    
Great description! Certainly deserves a 5!
Cheers
Andi
Sergey Alexandrovich Kryukov 26-Jul-14 21:25pm    
Thank you very much, Andi.
—SA
user8x86 27-Jul-14 1:18am    
This make me sad. But thank you, Sergey!
Sergey Alexandrovich Kryukov 27-Jul-14 1:32am    
You are very welcome.

But please don't get sad. Consider some criticism like mine not as a problem but as an opportunity. Bravely get to learning whatever you need to learn. Try to be based more on facts then on the guesses, at least before you acquire better experience. Your mistakes are common, not fatal.

And remember that many experts here will gladly try to answer your questions.

Good luck, call again.

—SA

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