windows DLL 文件究竟是什么,它们是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/124549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:22:27  来源:igfitidea点击:

What exactly are DLL files, and how do they work?

windowswinapidll

提问by stalepretzel

How exactly do DLL files work? There seems to be an awful lot of them, but I don't know what they are or how they work.

DLL 文件究竟是如何工作的?它们似乎有很多,但我不知道它们是什么或它们如何工作。

So, what's the deal with them?

那么,和他们有什么关系?

回答by Brian R. Bondy

What is a DLL?

什么是 DLL?

Dynamic Link Libraries (DLL)s are like EXEs but they are not directly executable. They are similar to .so files in Linux/Unix. That is to say, DLLs are MS's implementation of shared libraries.

动态链接库 (DLL) 类似于 EXE,但它们不能直接执行。它们类似于 Linux/Unix 中的 .so 文件。也就是说,DLL 是 MS 对共享库的实现。

DLLs are so much like an EXE that the file format itself is the same. Both EXE and DLLs are based on the Portable Executable (PE) file format. DLLs can also contain COM components and .NET libraries.

DLL 与 EXE 非常相似,以至于文件格式本身是相同的。EXE 和 DLL 都基于可移植可执行 (PE) 文件格式。DLL 还可以包含 COM 组件和 .NET 库。

What does a DLL contain?

DLL 包含什么?

A DLL contains functions, classes, variables, UIs and resources (such as icons, images, files, ...) that an EXE, or other DLL uses.

DLL 包含 EXE 或其他 DLL 使用的函数、类、变量、UI 和资源(例如图标、图像、文件等)。

Types of libraries:

图书馆类型:

On virtually all operating systems, there are 2 types of libraries. Static libraries and dynamic libraries. In windows the file extensions are as follows: Static libraries (.lib) and dynamic libraries (.dll). The main difference is that static libraries are linked to the executable at compile time; whereas dynamic linked libraries are not linked until run-time.

在几乎所有操作系统上,都有两种类型的库。静态库和动态库。在 Windows 中,文件扩展名如下:静态库 (.lib) 和动态库 (.dll)。主要区别在于静态库在编译时链接到可执行文件;而动态链接库直到运行时才链接。

More on static and dynamic libraries:

更多关于静态和动态库:

You don't normally see static libraries though on your computer, because a static library is embedded directly inside of a module (EXE or DLL). A dynamic library is a stand-alone file.

您通常不会在计算机上看到静态库,因为静态库直接嵌入在模块(EXE 或 DLL)中。动态库是一个独立的文件。

A DLL can be changed at any time and is only loaded at runtime when an EXE explicitly loads the DLL. A static library cannot be changed once it is compiled within the EXE. A DLL can be updated individually without updating the EXE itself.

DLL 可以随时更改,并且仅在 EXE 显式加载 DLL 时才在运行时加载。静态库一旦在 EXE 中编译就不能更改。可以单独更新 DLL,而无需更新 EXE 本身。

Loading a DLL:

加载 DLL:

A program loads a DLL at startup, via the Win32 API LoadLibrary, or when it is a dependency of another DLL. A program uses the GetProcAddress to load a function or LoadResource to load a resource.

程序在启动时通过 Win32 API LoadLibrary 或当它是另一个 DLL 的依赖项时加载 DLL。程序使用 GetProcAddress 加载函数或使用 LoadResource 加载资源。

Further reading:

进一步阅读:

Please check MSDNor Wikipediafor further reading. Also the sources of this answer.

请查看MSDNWikipedia以进一步阅读。也是这个答案的来源。

回答by computinglife

What is a DLL?

什么是 DLL?

DLL files are binary files that can contain executable code and resources like images, etc. Unlike applications, these cannot be directly executed, but an application will load them as and when they are required (or all at once during startup).

DLL 文件是二进制文件,可以包含可执行代码和图像等资源。与应用程序不同,它们不能直接执行,但应用程序会在需要时加载它们(或在启动期间一次性加载)。

Are they important?

它们重要吗?

Most applications will load the DLL files they require at startup. If any of these are not found the system will not be able to start the process at all.

大多数应用程序会在启动时加载它们需要的 DLL 文件。如果未找到其中任何一项,系统将根本无法启动该过程。

DLL files might require other DLL files

DLL 文件可能需要其他 DLL 文件

In the same way that an application requires a DLL file, a DLL file might be dependent on other DLL files itself. If one of these DLL files in the chain of dependency is not found, the application will not load. This is debugged easily using any dependency walker tools, like Dependency Walker.

与应用程序需要 DLL 文件的方式相同,DLL 文件可能依赖于其他 DLL 文件本身。如果在依赖链中找不到这些 DLL 文件之一,应用程序将不会加载。使用任何Dependency Walker工具(例如Dependency Walker )可以轻松调试。

There are so many of them in the system folders

系统文件夹里有这么多

Most of the system functionality is exposed to a user program in the form of DLL files as they are a standard form of sharing code / resources. Each functionality is kept separately in different DLL files so that only the required DLL files will be loaded and thus reduce the memory constraints on the system.

大多数系统功能都以 DLL 文件的形式向用户程序公开,因为它们是共享代码/资源的标准形式。每个功能都分别保存在不同的 DLL 文件中,以便仅加载所需的 DLL 文件,从而减少系统上的内存限制。

Installed applications also use DLL files

已安装的应用程序也使用 DLL 文件

DLL files also becomes a form of separating functionalities physically as explained above. Good applications also try to not load the DLL files until they are absolutely required, which reduces the memory requirements. This too causes applications to ship with a lot of DLL files.

如上所述,DLL 文件也成为一种物理分离功能的形式。好的应用程序也会尝试在绝对需要之前不加载 DLL 文件,这会降低内存需求。这也会导致应用程序附带大量 DLL 文件。

DLL Hell

DLL 地狱

However, at times system upgrades often breaks other programs when there is a version mismatch between the shared DLL files and the program that requires them. System checkpoints and DLL cache, etc. have been the initiatives from M$ to solve this problem. The .NET platform might not face this issue at all.

但是,当共享 DLL 文件与需要它们的程序之间存在版本不匹配时,有时系统升级通常会中断其他程序。系统检查点和DLL缓存等一直是M$解决这个问题的举措。.NET 平台可能根本不会面临这个问题。

How do we know what's inside a DLL file?

我们如何知道 DLL 文件中的内容?

You have to use an external tool like DUMPBINor Dependency Walker which will not only show what publicly visible functions (known as exports) are contained inside the DLL files and also what other DLL files it requires and which exports from those DLL files this DLL file is dependent upon.

您必须使用像DUMPBIN或 Dependency Walker 这样的外部工具,它不仅会显示 DLL 文件中包含哪些公开可见的函数(称为导出),以及它需要哪些其他 DLL 文件,以及从这些 DLL 文件导出此 DLL 文件的内容是依赖的。

How do we create / use them?

我们如何创建/使用它们?

Refer the programming documentation from your vendor. For C++, refer to LoadLibraryin MSDN.

请参阅供应商提供的编程文档。对于 C++,请参阅MSDN 中的LoadLibrary

回答by jussij

Let's say you are making an executable that uses some functions found in a library.

假设您正在制作一个使用库中某些函数的可执行文件。

If the library you are using is static, the linker will copy the object code for these functions directly from the library and insert them into the executable.

如果您使用的库是static,链接器将直接从库中复制这些函数的目标代码并将它们插入到可执行文件中。

Now if this executable is run it has every thing it needs, so the executable loader just loads it into memory and runs it.

现在,如果运行这个可执行文件,它就拥有它需要的所有东西,所以可执行文件加载器只是将它加载到内存中并运行它。

If the library is dynamicthe linker will not insert object code but rather it will insert a stub which basically says this function is located in this DLLat this location.

如果库是动态的,链接器将不会插入目标代码,而是会插入一个存根,该存根基本上表示此函数位于此DLL中的此位置。

Now if this executable is run, bits of the executable are missing (i.e the stubs) so the loader goes through the executable fixing up the missing stubs. Only after all the stubs have been resolved will the executable be allowed to run.

现在,如果此可执行文件运行,可执行文件的位丢失(即存根),因此加载程序通过可执行文件修复丢失的存根。只有在所有存根都被解析后,可执行文件才被允许运行。

To see this in action delete or rename the DLL and watch how the loader will report a missing DLL error when you try to run the executable.

要查看此操作,请删除或重命名 DLL,并观察加载程序如何在您尝试运行可执行文件时报告缺少 DLL 错误。

Hence the name Dynamic Link Library, parts of the linking process is being done dynamically at run time by the executable loader.

因此名称为动态链接库,链接过程的一部分是由可执行加载器在运行时动态完成的。

One a final note, if you don't link to the DLL then no stubs will be inserted by the linker, but Windows still provides the GetProcAddressAPI that allows you to load an execute the DLL function entry point long after the executable has started.

最后要注意的是,如果您不链接到 DLL,则链接器不会插入任何存根,但 Windows 仍提供GetProcAddressAPI,允许您在可执行文件启动后很长时间加载执行 DLL 函数入口点。

回答by paxdiablo

DLLs (dynamic link libraries) and SLs (shared libraries, equivalent under UNIX) are just libraries of executable code which can be dynamically linked into an executable at load time.

DLL(动态链接库)和 SL(共享库,在 UNIX 下等价)只是可执行代码库,它们可以在加载时动态链接到可执行文件中。

Static libraries are inserted into an executable at compile time and are fixed from that point. They increase the size of the executable and cannot be shared.

静态库在编译时插入到可执行文件中,并从那时起修复。它们增加了可执行文件的大小并且无法共享。

Dynamic libraries have the following advantages:

动态库具有以下优点:

1/ They are loaded at run time rather than compile time so they can be updated independently of the executable (all those fancy windows and dialog boxes you see in Windows come from DLLs so the look-and-feel of your application can change without you having to rewrite it).

1/ 它们在运行时而不是编译时加载,因此它们可以独立于可执行文件进行更新(您在 Windows 中看到的所有那些花哨的窗口和对话框都来自 DLL,因此您的应用程序的外观可以在没有您的情况下改变必须重写它)。

2/ Because they're independent, the code can be shared across multiple executables - this saves memory since, if you're running 100 apps with a single DLL, there may only be one copy of the DLL in memory.

2/ 因为它们是独立的,所以代码可以在多个可执行文件之间共享——这可以节省内存,因为如果你用一个 DLL 运行 100 个应用程序,内存中可能只有一个 DLL 副本。

Their main disadvantage is advantage #1 - having DLLs change independent your application may cause your application to stop working or start behaving in a bizarre manner. DLL versioning tend not to be managed very well under Windows and this leads to the quaintly-named "DLL Hell".

它们的主要缺点是优势 #1 - 让 DLL 独立于您的应用程序更改可能会导致您的应用程序停止工作或开始以奇怪的方式运行。DLL 版本控制在 Windows 下往往无法很好地管理,这导致了名称古怪的“DLL 地狱”。

回答by Adam Pierce

DLL files contain an Export Tablewhich is a list of symbols which can be looked up by the calling program. The symbols are typically functions with the C calling convention(__stcall). The export table also contains the address of the function.

DLL 文件包含一个导出表,它是一个可由调用程序查找的符号列表。这些符号通常是具有C 调用约定( __stcall) 的函数。导出表还包含函数的地址。

With this information, the calling program can then call the functions within the DLL even though it did not have access to the DLL at compile time.

有了这些信息,调用程序就可以调用 DLL 中的函数,即使它在编译时没有访问 DLL 的权限。

Introducing Dynamic Link Librarieshas some more information.

Introducing Dynamic Link Libraries提供了更多信息。

回答by Jorge Ferreira

http://support.microsoft.com/kb/815065

http://support.microsoft.com/kb/815065

A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Therefore, each program can use the functionality that is contained in this DLL to implement an Open dialog box. This helps promote code reuse and efficient memory usage.

By using a DLL, a program can be modularized into separate components. For example, an accounting program may be sold by module. Each module can be loaded into the main program at run time if that module is installed. Because the modules are separate, the load time of the program is faster, and a module is only loaded when that functionality is requested.

Additionally, updates are easier to apply to each module without affecting other parts of the program. For example, you may have a payroll program, and the tax rates change each year. When these changes are isolated to a DLL, you can apply an update without needing to build or install the whole program again.

DLL 是一种库,其中包含可供多个程序同时使用的代码和数据。例如,在 Windows 操作系统中,Comdlg32 DLL 执行常见的对话框相关功能。因此,每个程序都可以使用此 DLL 中包含的功能来实现打开对话框。这有助于促进代码重用和有效的内存使用。

通过使用 DLL,可以将程序模块化为单独的组件。例如,会计程序可以按模块出售。如果安装了该模块,则每个模块都可以在运行时加载到主程序中。由于模块是独立的,程序的加载时间更快,并且仅在请求该功能时才加载模块。

此外,更新更容易应用于每个模块,而不会影响程序的其他部分。例如,您可能有一个工资计划,税率每年都在变化。当这些更改与 DLL 隔离时,您可以应用更新而无需再次构建或安装整个程序。

http://en.wikipedia.org/wiki/Dynamic-link_library

http://en.wikipedia.org/wiki/Dynamic-link_library

回答by Akshya Nagar

DLL is a File Extension & Known As “dynamic link library” file format used for holding multiple codes and procedures for Windows programs. Software & Games runs on the bases of DLL Files; DLL files was created so that multiple applications could use their information at the same time.

DLL 是一种文件扩展名,称为“动态链接库”文件格式,用于保存 Windows 程序的多个代码和过程。软件和游戏基于 DLL 文件运行;创建 DLL 文件是为了让多个应用程序可以同时使用它们的信息。

IF you want to get more information about DLL Files or facing any error read the following post. https://www.bouncegeek.com/fix-dll-errors-windows-586985/

如果您想获得有关 DLL 文件的更多信息或遇到任何错误,请阅读以下帖子。 https://www.bouncegeek.com/fix-dll-errors-windows-586985/

回答by code builders

According to Microsoft

根据微软

(DLL) Dynamic link libraries are files that contain data, code, or resources needed for the running of applications. These are files that are created by the windows ecosystem and can be shared between two or more applications.

(DLL) 动态链接库是包含运行应用程序所需的数据、代码或资源的文件。这些是由 Windows 生态系统创建的文件,可以在两个或多个应用程序之间共享。

When a program or software runs on Windows, much of how the application works depends on the DLL files of the program. For instance, if a particular application had several modules, then how each module interacts with each other is determined by the Windows DLL files.

当程序或软件在 Windows 上运行时,应用程序的大部分工作方式取决于程序的 DLL 文件。例如,如果一个特定的应用程序有多个模块,那么每个模块如何相互交互是由 Windows DLL 文件决定的。

If you want detailed explanation, check these useful resources

如果您需要详细解释,请查看这些有用的资源

What are dll files, About Dll files

什么是dll文件关于dll文件

回答by tsilb

DLLs (Dynamic Link Libraries) contain resources used by one or more applications or services. They can contain classes, icons, strings, objects, interfaces, and pretty much anything a developer would need to store except a UI.

DLL(动态链接库)包含一个或多个应用程序或服务使用的资源。它们可以包含类、图标、字符串、对象、接口以及开发人员需要存储的几乎所有内容,除了 UI。