windows .dll 和 .exe 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1210873/
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
Difference between .dll and .exe?
提问by Umesh Aawte
I want to know the exact difference between the dll and exe file.
我想知道 dll 和 exe 文件之间的确切区别。
采纳答案by firstthumb
EXE:
可执行程序:
- It's a executable file
- When loading an executable, no export is called, but only the module entry point.
- When a system launches new executable, a new process is created
- The entry thread is called in context of main thread of that process.
- 这是一个可执行文件
- 加载可执行文件时,不会调用导出,而只会调用模块入口点。
- 当系统启动新的可执行文件时,会创建一个新进程
- 在该进程的主线程的上下文中调用入口线程。
DLL:
动态链接库:
- It's a Dynamic Link Library
- There are multiple exported symbols.
- The system loads a DLL into the context of an existing process.
- 这是一个动态链接库
- 有多个导出的符号。
- 系统将 DLL 加载到现有进程的上下文中。
For More Details: http://www.c-sharpcorner.com/Interviews/Answer/Answers.aspxQuestionId=1431&MajorCategoryId=1&MinorCategoryId=1http://wiki.answers.com/Q/What_is_the_difference_between_an_EXE_and_a_DLL
更多详情:http: //www.c-sharpcorner.com/Interviews/Answer/Answers.aspxQuestionId=1431&MajorCategoryId=1&MinorCategoryId=1 http://wiki.answers.com/Q/What_is_the_difference_between_an_EXE_and_a_DLL
Reference: http://www.dotnetspider.com/forum/34260-What-difference-between-dll-exe.aspx
参考:http: //www.dotnetspider.com/forum/34260-What-difference-between-dll-exe.aspx
回答by Aamir
I don't know why everybody is answering this question in context of .NET. The question was a general one and didn't mention .NET anywhere.
我不知道为什么每个人都在 .NET 的上下文中回答这个问题。这个问题很笼统,并没有在任何地方提到 .NET。
Well, the major differences are:
嗯,主要的区别是:
EXE
可执行程序
- An exe always runs in its own address space i.e., It is a separate process.
- The purpose of an EXE is to launch a separate application of its own.
- exe 总是在它自己的地址空间中运行,即,它是一个单独的进程。
- EXE 的目的是启动它自己的单独应用程序。
DLL
动态链接库
- A dll always needs a host exe to run. i.e., it can never run in its own address space.
- The purpose of a DLL is to have a collection of methods/classes which can be re-used from some other application.
- DLL is Microsoft's implementation of a shared library.
- dll 总是需要一个主机 exe 才能运行。即,它永远不能在自己的地址空间中运行。
- DLL 的目的是拥有一组方法/类,这些方法/类可以从其他应用程序中重用。
- DLL 是 Microsoft 的共享库实现。
The file format of DLL and exe is essentially the same. Windows recognizes the difference between DLL and EXE through PE Header in the file. For details of PE Header, You can have a look at this Article on MSDN
DLL 和 exe 的文件格式本质上是一样的。Windows 通过文件中的 PE Header 来识别 DLL 和 EXE 之间的区别。有关 PE Header 的详细信息,您可以查看 MSDN 上的这篇文章
回答by Robin Day
The difference is that an EXE has an entry point, a "main" method that will run on execution.
不同之处在于 EXE 有一个入口点,一个将在执行时运行的“主”方法。
The code within a DLL needs to be called from another application.
DLL 中的代码需要从另一个应用程序调用。
回答by kichik
There are a few more differences regarding the structure you could mention.
关于您可以提到的结构,还有一些不同之处。
- Both DLL and EXE share the same file structure - Portable Executable, or PE. To differentiate between the two, one can look in the
Characteristics
member ofIMAGE_FILE_HEADER
insideIMAGE_NT_HEADERS
. For a DLL, it has theIMAGE_FILE_DLL
(0x2000) flag turned on. For a EXE it's theIMAGE_FILE_EXECUTABLE_IMAGE
(0x2) flag. - PE files consist of some headers and a number of sections. There's usually a section for code, a section for data, a section listing imported functions and a section for resources. Some sections may contain more than one thing. The header also describes a list of data directories that are located in the sections. Those data directories are what enables Windows to find what it needs in the PE. But one type of data directory that an EXE will never have (unless you're building a frankenstein EXE) is the export directory. This is where DLL files have a list of functions they export and can be used by other EXE or DLL files. On the other side, each DLL and EXE has an import directory where it lists the functions and DLL files it requires to run.
- Also in the PE headers (
IMAGE_OPTIONAL_HEADER
) is theImageBase
member. It specifies the virtual address at which the PE assumes it will be loaded. If it is loaded at another address, some pointers could point to the wrong memory. As EXE files are amongst the first to be loaded into their new address space, the Windows loader can assure a constant load address and that's usually 0x00400000. That luxury doesn't exist for a DLL. Two DLL files loaded into the same process can request the same address. This is why a DLL has another data directory called Base Relocation Directory that usually resides in its own section -.reloc
. This directory contains a list of places in the DLL that need to be rebased/patched so they'll point to the right memory. Most EXE files don't have this directory, but some old compilers do generate them.
- DLL 和 EXE 共享相同的文件结构 - Portable Executable 或 PE。要区分这两者,可以查看inside的
Characteristics
成员。对于 DLL,它打开了(0x2000) 标志。对于 EXE,它是(0x2) 标志。IMAGE_FILE_HEADER
IMAGE_NT_HEADERS
IMAGE_FILE_DLL
IMAGE_FILE_EXECUTABLE_IMAGE
- PE 文件由一些标题和许多部分组成。通常有一个代码部分、一个数据部分、一个列出导入函数的部分和一个资源部分。某些部分可能包含不止一件事。标题还描述了位于各节中的数据目录列表。这些数据目录使 Windows 能够在 PE 中找到它需要的内容。但是 EXE 永远不会拥有的一种类型的数据目录(除非您正在构建一个科学怪人 EXE)是导出目录。这是 DLL 文件具有它们导出的函数列表并且可以被其他 EXE 或 DLL 文件使用的地方。另一方面,每个 DLL 和 EXE 都有一个导入目录,其中列出了运行所需的函数和 DLL 文件。
- 同样在 PE 标头 (
IMAGE_OPTIONAL_HEADER
) 中的是ImageBase
成员。它指定 PE 假定将加载的虚拟地址。如果它被加载到另一个地址,一些指针可能指向错误的内存。由于 EXE 文件是最先加载到新地址空间的文件,Windows 加载程序可以确保加载地址恒定,通常为 0x00400000。对于 DLL 来说,这种奢侈是不存在的。加载到同一个进程中的两个 DLL 文件可以请求相同的地址。这就是 DLL 具有另一个数据目录的原因,该目录通常位于其自己的部分 -.reloc
. 该目录包含 DLL 中需要重新定位/修补的位置列表,以便它们指向正确的内存。大多数 EXE 文件没有这个目录,但一些旧的编译器会生成它们。
You can read more on this topic @ MSDN.
您可以在@MSDN上阅读有关此主题的更多信息。
回答by hannson
This answer was a little more detailed than I thought but read it through.
这个答案比我想象的要详细一些,但请通读一遍。
DLL:
In most cases, a DLL file is a library. There are a couple of types of libraries, dynamic and static - read about the difference. DLL stands for dynamic link librarywhich tells us that it's a partof the program but notthe whole thing. It's made of reusable software components (library) which you could use for more than a single program. Bear in mind that it's always possible to use the library source code in many applications using copy-paste, but the idea of a DLL/Static Library is that you could update the code of a library and at the same time update all the applications using it - without compiling.
DLL:
在大多数情况下,DLL 文件是一个库。有几种类型的库,动态的和静态的 -阅读区别。DLL 代表动态链接库,它告诉我们它是程序的一部分,而不是整个程序。它由可重用的软件组件(库)组成,您可以将其用于多个程序。请记住,始终可以使用复制粘贴在许多应用程序中使用库源代码,但 DLL/静态库的想法是您可以更新库的代码,同时使用它 - 无需编译。
For example:
Imagine you're creating a Windows GUI componentlike a Button. In most cases you'd want to re-use the code you've written because it's a complex but a commoncomponent - You want many applications to use it but you don't want to give them the source code You can't copy-paste the code for the button in every program, so you decide you want to create a DL-Library (DLL).
例如:
假设您正在创建一个Windows GUI 组件,如Button。在大多数情况下,您希望重新使用您编写的代码,因为它是一个复杂但通用的组件 - 您希望许多应用程序使用它,但又不想向它们提供源代码 您无法复制- 在每个程序中粘贴按钮的代码,以便您决定要创建一个DL-Library (DLL)。
This "button"library is required by EXEcutables to run, and without it they will not run because they don't know how to create the button, only how to talk to it.
EXEcutables需要这个“按钮”库才能运行,没有它它们将无法运行,因为它们不知道如何创建按钮,只知道如何与它对话。
Likewise, a DLL cannot be executed - run, because it's only a part of the program but doesn't have the information required to create a "process".
同样,DLL 不能被执行 - 运行,因为它只是程序的一部分,但没有创建“进程”所需的信息。
EXE:
An executable is the program. It knows how to create a processand how to talk to the DLL. It needsthe DLL to create a button, and without it the application doesn't run - ERROR.
EXE:
可执行文件是程序。它知道如何创建进程以及如何与 DLL 对话。它需要DLL 来创建按钮,如果没有它,应用程序将无法运行 - 错误。
hope this helps....
希望这可以帮助....
回答by Zaheer Ahmed
Both DLL and EXE are Portable Executable(PE) Formats
DLL 和 EXE 都是可移植的可执行 (PE) 格式
A Dynamic-link library (DLL)is a library and therefore can not be executed directly. If you try to run it you will get an error about a missing entry point. It needs an entry point (main function) to get executed, that entry point can be any application or exe. DLL binding occurs at run-time. That is why its called "Dynamic Link" library.
甲动态链接库(DLL)是一个库,因此无法直接执行。如果您尝试运行它,您将收到有关缺少入口点的错误。它需要一个入口点(主函数)来执行,入口点可以是任何应用程序或 exe。DLL 绑定发生在运行时。这就是为什么它被称为“动态链接”库。
An Executable (EXE)is a program that can be executed. It has its own entry point. A flag inside the PE header indicates which type of file it is (irrelevant of file extension). The PE header has a field where the entry point for the program resides. In DLLs it isn't used (or at least not as an entry point).
一个可执行文件(EXE)是可被执行的程序。它有自己的入口点。PE 标头内的标志指示它是哪种类型的文件(与文件扩展名无关)。PE 标头有一个字段,程序的入口点位于该字段中。在 DLL 中不使用它(或至少不用作入口点)。
There are many softwareavailable to check header information. The only difference causing both to work differently is the bit in header as shown in below diagram.
有许多软件可用于检查标题信息。导致两者工作不同的唯一区别是标题中的位,如下图所示。
EXE file has only single main entry means it is isolated application, when a system launches exe, a new process is created while DLLs have many entry points so when application use it no new process started, DLL can be reused and versioned. DLL reduces storage space as different programs can use the same dll.
EXE 文件只有一个主入口意味着它是一个独立的应用程序,当系统启动 exe 时,会创建一个新进程,而 DLL 有许多入口点,因此当应用程序使用它时,不会启动新进程,DLL 可以被重用和版本化。DLL 减少了存储空间,因为不同的程序可以使用相同的 dll。
回答by skanda93
Dll v/s Exe
dll v/s exe
1)DLL file is a dynamic link library which can be used in exe files and
other dll files.
EXE file is a executable file which runs in a separate
process which is managed by OS.
1)DLL文件是一个动态链接库,可以在exe文件和其他dll文件中使用。
EXE 文件是一个可执行文件,它在由操作系统管理的单独进程中运行。
2)DLLs are not directly executable . They are separate files containing functions that can be called by programs and other DLLs to perform computations and functions.
An EXE is a program that can be executed . Ex :Windows program
2)DLL 不能直接执行。它们是单独的文件,其中包含可由程序和其他 DLL 调用以执行计算和函数的函数。
EXE 是可以执行的程序。例如:Windows 程序
3)Reusability
DLL: They can be reused for some other application. As long as the coder knows the names and parameters of the functions and procedures in the DLL file .
EXE: Only for specific purpose .
3) 可重用性
DLL:它们可以重用于其他一些应用程序。只要编码人员知道 DLL 文件中的函数和过程的名称和参数。
EXE:仅用于特定目的。
4)A DLL would share the same process and memory space of the calling application while an
EXE creates its separate process and memory space.
4) DLL 将共享调用应用程序的相同进程和内存空间,而
EXE 创建其单独的进程和内存空间。
5)Uses
DLL: You want many applications to use it but you don't want to give them the source code You can't copy-paste the code for the button in every program, so you decide you want to create a DL-Library (DLL).
5) 使用
DLL:您希望许多应用程序使用它,但又不想给它们源代码 您无法在每个程序中复制粘贴按钮的代码,因此您决定要创建一个 DL-库 (DLL)。
EXE: When we work with project templates like Windows Forms Applications, Console Applications, WPF Applications and Windows Services they generate an exe assembly when compiled.
EXE:当我们使用 Windows 窗体应用程序、控制台应用程序、WPF 应用程序和 Windows 服务等项目模板时,它们会在编译时生成一个 exe 程序集。
6)Similarities :
Both DLL and EXE are binary files have a complex nested structure defined by the Portable Executable format, and they are not intended to be editable by users.
6) 相似之处:
DLL 和EXE 都是二进制文件,具有由Portable Executable 格式定义的复杂嵌套结构,它们不能被用户编辑。
回答by judy smith
Two things: the extension and the header flag stored in the file.
两件事:文件中存储的扩展名和标头标志。
Both files are PE files. Both contain the exact same layout. A DLL is a library and therefore can not be executed. If you try to run it you'll get an error about a missing entry point. An EXE is a program that can be executed. It has an entry point. A flag inside the PE header indicates which file type it is (irrelevant of file extension). The PE header has a field where the entry point for the program resides. In DLLs it isn't used (or at least not as an entry point).
这两个文件都是PE文件。两者都包含完全相同的布局。DLL 是一个库,因此无法执行。如果您尝试运行它,您将收到有关缺少入口点的错误。EXE 是可以执行的程序。它有一个入口点。PE 标头内的标志指示它是哪种文件类型(与文件扩展名无关)。PE 标头有一个字段,程序的入口点位于该字段中。在 DLL 中不使用它(或至少不用作入口点)。
One minor difference is that in most cases DLLshave an export section where symbols are exported. EXEs should never have an export section since they aren't libraries but nothing prevents that from happening. The Win32 loader doesn't care either way.
一个微小的区别是,在大多数情况下,DLL都有一个导出部分,用于导出符号。EXE 永远不应该有导出部分,因为它们不是库,但没有什么可以阻止这种情况发生。Win32 加载器不关心任何一种方式。
Other than that they are identical. So, in summary, EXEs are executable programs while DLLs are libraries loaded into a process and contain some sort of useful functionality like security, database access or something.
除此之外,它们是相同的。因此,总而言之,EXE 是可执行程序,而 DLL 是加载到进程中的库,包含某种有用的功能,如安全性、数据库访问等。
回答by rahul
An EXE is visible to the system as a regular Win32 executable. Its entry point refers to a small loader which initializes the .NET runtime and tells it to load and execute the assembly contained in the EXE. A DLL is visible to the system as a Win32 DLL but most likely without any entry points. The .NET runtime stores information about the contained assembly in its own header.
EXE 作为常规 Win32 可执行文件对系统可见。它的入口点是指一个小的加载器,它初始化 .NET 运行时并告诉它加载和执行 EXE 中包含的程序集。DLL 作为 Win32 DLL 对系统可见,但很可能没有任何入口点。.NET 运行时将有关包含的程序集的信息存储在其自己的标头中。
dll is a collection of reusable functions where as an .exe is an executable which may call these functions
dll 是可重用函数的集合,其中 .exe 是可以调用这些函数的可执行文件
回答by Steve314
The .exe is the program. The .dll is a library that a .exe (or another .dll) may call into.
.exe 是程序。.dll 是一个 .exe(或另一个 .dll)可以调用的库。
What sakthivignesh says can be true in that one .exe can use another as if it were a library, and this is done (for example) with some COM components. In this case, the "slave" .exe is a separate program (strictly speaking, a separate process - perhaps running on a separate machine), but one that accepts and handles requests from other programs/components/whatever.
sakthivignesh 所说的可能是正确的,因为一个 .exe 可以像使用库一样使用另一个 .exe,并且这是(例如)使用某些 COM 组件完成的。在这种情况下,“从”.exe 是一个单独的程序(严格来说,是一个单独的进程——可能运行在一台单独的机器上),但它接受和处理来自其他程序/组件/任何东西的请求。
However, if you just pick a random .exe and .dll from a folder in your Program Files, odds are that COM isn't relevant - they are just a program and its dynamically-linked libraries.
但是,如果您只是从 Program Files 中的文件夹中随机选择一个 .exe 和 .dll,则很可能与 COM 无关——它们只是一个程序及其动态链接的库。
Using Win32 APIs, a program can load and use a DLL using the LoadLibrary and GetProcAddress API functions, IIRC. There were similar functions in Win16.
使用 Win32 API,程序可以使用 LoadLibrary 和 GetProcAddress API 函数 IIRC 加载和使用 DLL。Win16中也有类似的功能。
COM is in many ways an evolution of the DLL idea, originally concieved as the basis for OLE2, whereas .NET is the descendant of COM. DLLs have been around since Windows 1, IIRC. They were originally a way of sharing binary code (particularly system APIs) between multiple running programs in order to minimise memory use.
COM 在许多方面是 DLL 思想的演变,最初被认为是 OLE2 的基础,而 .NET 是 COM 的后代。DLL 从 Windows 1、IIRC 开始就已经存在。它们最初是一种在多个正在运行的程序之间共享二进制代码(特别是系统 API)以最小化内存使用的方式。