动态链接库(DLL)如何工作? [英] How does Dynamic linking library (DLL) work?

查看:113
本文介绍了动态链接库(DLL)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



i有一个很好的DLL(动态链接库)麻烦。

据我所知,当*。加载exe,操作系统将检查程序的DLL文件。

如果找到DLL =>检查继续:-)



如果程序需要的DLL代码已加载到内存中=>跳过

Else =>将DLL中的下一个代码加载到memory =>继续。



更多的知识,在运行之前,exe文件必须由编译器编译为汇编代码。

这意味着,编译器是一个打开exe文件的工具。因此,在加载时,编译器打开并将段代码(DLL代码的一部分)带到OS。然后OS =>加载DLL文件=>写入内存。



我的麻烦是:

我的理解是真的吗?

收到的代码操作系统是public static void Main()或1111010010101?

And ...我可以编写代码以便像C#编译器一样打开和带代码(不是设计新的编译器,它太复杂了)?



// p / s:我已经下载了GCC编译器源代码。但我不知道我需要什么代码^^!



请!

非常感谢你!

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!

推荐答案

exe文件必须由编译器编译为汇编代码



No.



编译器是打开exe文件的工具



No.




最接近的是,当使用DLL时,编译器需要访问DLL的公共可用符号。但那是在编译时,而不是在运行时。





我希望其他人回复一个好的解释链接。
"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.


首先,让我祝贺你有一个好麻烦。毫无疑问,有一个丑陋的麻烦会更糟糕。



让我继续解决方案1.



......当加载* .exe时,操作系统将检查程序的DLL文件。



编号该语句没有意义,因为没有诸如程序的DLL文件之类的东西。



编译器是一个打开exe文件的工具。



编号编译器是一个将源代码编译成目标代码的工具(无论它是什么)。



编译器打开并带来段代码...... 。



编号编译器没有这样做。它读取源代码并创建其他文件,编译结果。



我的理解是真的吗?



不,显然不是。



我是否可以编写代码以便像C#编译器一样打开和带代码...



编号原因有两个:1)编译器没有这样做; 2)你对所提到的一切的理解是错误的。



最后,让我们回到你的主要问题:DLL如何工作。



真的理解它,我的答案是不够的,因为这不是你需要先学习的东西。首先,你需要学习如何...其他一切工作:EXE,应用程序,进程,段,内存,CPU,编译器,链接器,加载器......你说出来了。你似乎太困惑了。我只能给你一些想法。



首先,DLL,EXE文件大致相同事情。DLL和EXE,作为文件名的一部分,只是命名约定,有m更像这样的扩展,它们都涵盖了一个常见的事情:PE文件:

http:// en.wikipedia.org/wiki/Portable_Executable [ ^ ]。



文件的结构非常复杂,如果你阅读了适当的文献,你可以学习它。它们通常只称为可执行文件,包括DLL。所有这些文件都包含可执行代码,而不仅仅是某些CPU 指令集架构的本机代码。构建为.NET程序集的PE模块(或程序集使用的单独模块,也可能但很少使用)包含IL代码,在运行时(通常基于每个方法)逐个编译为本机代码。 />


动态链接库的主要区别在于它们可能有也可能没有入口点(并且它们可能具有的入口点具有不同的签名和与EXE不同的目的)。这是一种加载系统已知的约定。操作系统的这一部分从Shell或OS调用获取文件名,并尝试加载文件并将其执行。入口点是执行开始的地方。操作系统将命令行参数和环境结构传递给进程,并使用定义的入口点启动它。因此,当OS加载EXE时,应该创建一个新进程。



对于DLL,其他PE模块引用。有一些规则可以通过名称为实际文件提供资金(.NET有一种称为强命名的特殊机制,它实际上可以使程序集世界独一无二;这并不总是使用)。应该在进程启动之前立即加载所有引用的DLL;如果没有,则错误诊断将在进程开始之前显示。



DLL也可以在运行时稍后加载。请参阅:

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

< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).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 [ ^ ](请参阅这些页面上的其他相关API函数)。



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



这些规则仅适用于标准操作系统加载东西和启动进程的方法。首先,EXE文件可以用作DLL,用于在正在运行的进程中加载​​并使用它们中的一些导出的代码。您可以开发自己的运行时系统,其中一些主机进程(可能最初以标准方式加载和启动)加载一些使用其特殊API构建的DLL,然后加载并用作可执行模块。类似的东西在不同的插件架构中真的完成。



参见:

http://en.wikipedia.org/wiki/Main_function [ ^ ],

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



与以下产品比较: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx [ ^ ]。



-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


这篇关于动态链接库(DLL)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆