简体   繁体   English

DLL中的内容是什么?

[英]What is in a DLL and how does it work?

I'm always referencing DLLs in my C# code, but they have remained somewhat of a mystery which I would like to clarify. 我总是在我的C#代码中引用DLL,但它们仍然有些神秘,我想澄清一下。 This is a sort of brain dump of questions regarding DLLs. 这是关于DLL的问题的一种大脑转储。

I understand a DLL is a dynamically linked library which means that another program can access this library at run time to get "functionality". 我理解DLL是一个动态链接库,这意味着另一个程序可以在运行时访问该库以获得“功能”。 However, consider the following ASP.NET project with Web.dll and Business.dll ( Web.dll is the front end functionality and it references Business.dll for types and methods). 但是,请考虑以下带有Web.dllBusiness.dll ASP.NET项目( Web.dll是前端功能,它引用了类型和方法的Business.dll )。

  1. At what point does Web.dll dynamically link to Business.dll ? Web.dll在什么时候动态链接到Business.dll You notice a lot in Windows HDD thrashing for seemingly small tasks when using Word (etc.) and I reckon that Word is going off and dynamically linking in functionality from other DLLs? 在使用Word(等)时,你会注意到很多Windows硬盘驱动程序看似小的任务,我认为Word正在关闭并动态链接其他DLL的功能?

    1a. 1A。 Additionally, what loads and links the DLL - the OS or some run time framework such as the .NET framework? 另外,什么加载和链接DLL - 操作系统或某些运行时框架,如.NET框架?

    1b. 1B。 What is the process of "linking"? 什么是“链接”的过程? Are compatibility checks made? 是否进行兼容性检查? Loading into the same memory? 加载到同一个内存? What does linking actually mean? 链接实际意味着什么?

  2. What actually executes the code in the DLL? 什么实际执行DLL中的代码? Does it get executed by the processor or is there another stage of translation or compilation before the processor will understand the code inside the DLL? 它是由处理器执行还是在处理器理解DLL中的代码之前还有另一个转换或编译阶段?

    2a. 2A。 In the case of a DLL built in C# .NET, what is running this: the .NET framework or the operating system directly? 对于使用C#.NET构建的DLL,运行它的是什么:.NET框架或操作系统直接?

  3. Does a DLL from Linux work on a Windows system (if such a thing exists), or are they operating system specific? Linux中的DLL是否可以在Windows系统上运行(如果存在这样的事情),或者它们是否特定于操作系统?

  4. Are DLLs specific to a particular framework? DLL是否特定于特定框架? Can a DLL built using C# .NET be used by a DLL built with, for example, Borland C++? 使用C#.NET构建的DLL是否可以由使用Borland C ++构建的DLL使用?

    4a. 4A。 If the answer to 4 is "no" then what is the point of a DLL? 如果4的答案是“否”那么DLL的重点是什么? Why dont the various frameworks use their own formats for linked files? 为什么各种框架不使用自己的格式来链接文件? For example: an .exe built in .NET knows that a file type of .abc is something that it can link into its code. 例如:.NET内置的.exe知道.abc的文件类型可以链接到其代码中。

  5. Going back to the Web.dll / Business.dll example - to get a class type of customer I need to reference Business.dll from Web.dll . 回到Web.dll / Business.dll示例 - 为了获得类型的客户,我需要从Web.dll引用Business.dll This must mean that Business.dll contains some sort of a specification as to what a customer class actually is. 这必须意味着Business.dll包含某种关于客户类实际是什么的规范。 If I had compiled my Business.dll file in, say, Delphi: would C# understand it and be able to create a customer class, or is there some sort of header info or something that says "hey sorry you can only use me from another Delphi DLL"? 如果我在Delphi中编译了我的Business.dll文件:C#会理解它并能够创建一个客户类,或者是否有某种标题信息或者说“对不起你只能用另一个人Delphi DLL“?

    5a. 5A。 Same applies for methods; 同样适用于方法; can I write a CreateInvoice() method in a DLL, compile it in C++, and then access and run it from C#? 我可以在DLL中编写CreateInvoice()方法,在C ++中编译,然后从C#访问并运行它吗? What stops or allows me from doing this? 什么阻止或允许我这样做?

  6. On the subject of DLL hijacking, surely the replacement (bad) DLL must contain the exact method signatures and types as the one that is being hijacked. 关于DLL劫持的主题,当然,替换(坏)DLL必须包含确切的方法签名和类型作为被劫持的方法。 I suppose this wouldn't be hard to do if you could find out what methods were available in the original DLL. 我想如果你能找到原始DLL中可用的方法,那就不难做到。

    6a. 6A。 What in my C# program is deciding if I can access another DLL? 在我的C#程序中决定我是否可以访问另一个DLL? If my hijacked DLL contained exactly the same methods and types as the original but it was compiled in another language, would it work? 如果我的被劫持的DLL包含与原始DLL完全相同的方法和类型但是它是用另一种语言编译的,它会起作用吗?

What is DLL importing and DLL registration? 什么是DLL导入和DLL注册?

First of all, you need to understand the difference between two very different kinds of DLLs. 首先,您需要了解两种非常不同的DLL之间的区别。 Microsoft decided to go with the same file extensions (.exe and .dll) with both .NET (managed code) and native code, however managed code DLLs and native DLLs are very different inside. Microsoft决定使用相同的文件扩展名(.exe和.dll)同时使用.NET(托管代码)和本机代码,但托管代码DLL和本机DLL在内部非常不同。

1) At what point does web.dll dynamically link to business.dll? 1)web.dll在什么时候动态链接到business.dll? You notice a lot in Windows HDD thrashing for seemingly small tasks when using Word etc and I reckon that this Word going off and dynamically linking in functionality from other DLL's? 在使用Word等时,你会注意到Windows硬盘驱动器中有很多看似小的任务,我认为这个Word会在其他DLL的功能中动态链接并动态链接?

1) In the case of .NET, DLLs are usually loaded on demand when the first method trying to access anything from the DLL is executed. 1)在.NET的情况下,当执行尝试从DLL访问任何内容的第一个方法时,DLL通常按需加载。 This is why you can get TypeNotFoundExceptions anywhere in your code if a DLL can't be loaded. 这就是为什么如果无法加载DLL,您可以在代码中的任何位置获取TypeNotFoundExceptions。 When something like Word suddenly starts accessing the HDD a lot, it's likely swapping (getting data that has been swapped out to the disk to make room in the RAM) 当像Word这样的东西突然开始大量访问硬盘时,它可能会交换(将已交换到磁盘的数据转换为在RAM中腾出空间)

1a) Additionally what loads and links the DLL - the O/S or some runtime framework such as the .Net framework? 1a)另外什么加载和链接DLL - O / S或某些运行时框架,如.Net框架?

1a) In the case of managed DLLs, the .NET framework is what loads, JIT compiles (compiles the .NET bytecode into native code) and links the DLLs. 1a)在托管DLL的情况下,.NET框架是加载,JIT编译(将.NET字节码编译成本机代码)并链接DLL。 In the case of native DLLs it's a component of the operating system that loads and links the DLL (no compilation is necessary because native DLLs already contain native code). 在本机DLL的情况下,它是操作系统的一个组件,用于加载和链接DLL(不需要编译,因为本机DLL已经包含本机代码)。

1b) What is the process of "linking"? 1b)“链接”的过程是什么? Are checks made that there is compatibility? 检查是否有兼容性? Loading into the same memory? 加载到同一个内存? What does linking actually mean? 链接实际意味着什么?

1b) Linking is when references (eg method calls) in the calling code to symbols (eg methods) in the DLL are replaced with the actual addresses of the things in the DLL. 1b)链接是指调用代码中的引用(例如方法调用)到DLL中的符号(例如方法)被DLL中的事物的实际地址替换。 This is necessary because the eventual addresses of the things in the DLL cannot be known before it's been loaded into memory. 这是必要的,因为在将DLL加载到内存之前,无法知道DLL中事物的最终地址。

2) What actually executes the code in the DLL? 2)实际执行DLL中的代码是什么? Does it get executed by the processor or is there another stage of translation or compilation before the processor will understand the code inside the DLL? 它是由处理器执行还是在处理器理解DLL中的代码之前还有另一个转换或编译阶段?

2) On Windows, .exe files and .dll files are quite identical. 2)在Windows上,.exe文件和.dll文件完全相同。 Native .exe and .dll files contain native code (the same stuff the processor executes), so there's no need to translate. 原生.exe和.dll文件包含本机代码(处理器执行的相同内容),因此无需翻译。 Managed .exe and .dll files contain .NET bytecode which is first JIT compiled (translated into native code). 托管的.exe和.dll文件包含.NET字节码,这是第一次JIT编译(翻译成本机代码)。

2a) In the case of a DLL built from C# .net what is running this? 2a)在使用C#.net构建的DLL的情况下运行这个是什么? The .Net framework or the operating system directly? .Net框架或操作系统直接?

2a) After the code has been JIT compiled, it's ran in the exact same way as any code. 2a)代码经过JIT编译后,它的运行方式与任何代码完全相同。

3) Does a DLL from say Linux work on a Windows system (if such a thing exists) or are they operating system specific? 3)说Linux的DLL是否适用于Windows系统(如果存在这样的东西)或者它们是否特定于操作系统?

3) Managed DLLs might work as-is, as long as the frameworks on both platforms are up to date and whoever wrote the DLL didn't deliberately break compatibility by using native calls. 3)托管DLL可以按原样工作,只要两个平台上的框架都是最新的,并且编写DLL的人不会故意通过使用本机调用来破坏兼容性。 Native DLLs will not works as-in, as the formats are different (even though the machine code inside is the same, if they're both for the same processor platform). 由于格式不同,原生DLL将无法正常工作(即使内部的机器代码相同,如果它们都属于同一处理器平台)。 By the way, on Linux, "DLLs" are known as .so (shared object) files. 顺便说一句,在Linux上,“DLL”被称为.so(共享对象)文件。

4) Are they specific to a particular framework? 4)它们是否特定于特定框架? Can a DLL built using C# .Net be used by a DLL built with Borland C++ (example only)? 使用C#.Net构建的DLL是否可以由使用Borland C ++构建的DLL(仅限示例)使用?

4) Managed DLLs are particular to the .NET framework, but naturally they work with any compatible language. 4)托管DLL是.NET框架特有的,但自然它们可以与任何兼容的语言一起使用。 Native DLLs are compatible as long as everyone uses the same conventions (calling conventions (how function arguments are passed on the machine code level), symbol naming, etc) 只要每个人都使用相同的约定(调用约定(如何在机器代码级别传递函数参数),符号命名等),本机DLL是兼容的

5) Going back to the web.dll / business.dll example. 5)回到web.dll / business.dll示例。 To get a class type of customer I need to reference business.dll from web.dll. 要获得类类型的客户,我需要从web.dll引用business.dll。 This must mean that business.dll contains a specification of some sort of what a customer class actually is. 这必须意味着business.dll包含某种客户类实际上的规范。 If I had compiled my business.dll file in say Delphi would C# understand it and be able to create a customer class - or is there some sort of header info or something that says "hey sorry you can only use me from another delphi dll". 如果我已经编译了我的business.dll文件,比如说Delphi会让C#理解它并能够创建一个客户类 - 或者是否有某种标题信息或者说“嘿抱歉你只能用我的另一个delphi dll” 。

5) Managed DLLs contain a full description of every class, method, field, etc they contain. 5)托管DLL包含它们包含的每个类,方法,字段等的完整描述。 AFAIK Delphi doesn't support .NET, so it would create native DLLs, which can't be used in .NET straightforwadly. AFAIK Delphi不支持.NET,因此它会创建本机DLL,而这些DLL不能直接在.NET中使用。 You will probably be able to call functions with PInvoke, but class definitions will not be found. 您可能能够使用PInvoke调用函数,但找不到类定义。 I don't use Delphi so I don't know how it stores type information with DLLs. 我不使用Delphi,所以我不知道它如何存储DLL的类型信息。 C++, for example, relies on header (.h) files which contain the type declarations and must be distributed with the DLL. 例如,C ++依赖于包含类型声明的头(.h)文件,并且必须与DLL一起分发。

6) On the subject of DLL hijacking, surely the replacement (bad) DLL must contain the exact method signatures, types as the one that is being hijacked. 6)关于DLL劫持的主题,当然,替换(坏)DLL必须包含确切的方法签名,类型是被劫持的类型。 I suppose this wouldnt be hard to do if you could find out what methods etc were available in the original DLL. 我想如果你能找到原始DLL中可用的方法等,那么这很难做到。

6) Indeed, it's not hard to do if you can easily switch the DLL. 6)实际上,如果你可以轻松切换DLL,这并不难。 Code signing can be used to avoid this. 代码签名可用于避免这种情况。 In order for someone to replace a signed DLL, they would have to know the signing key, which it kept secret. 为了让某人替换已签名的DLL,他们必须知道签名密钥,它保密。

6a) A bit of a repeat question here but this goes back to what in my C# program is deciding if I can access another DLL? 6a)这里有一个重复的问题,但这可以追溯到我的C#程序决定是否可以访问另一个DLL的内容? If my hijacked DLL contained exactly the same methods and types as the original but it was compiled in another lanugage would it work? 如果我的被劫持的DLL包含与原始DLL完全相同的方法和类型,但它是在另一个语言中编译的,它会起作用吗?

6a) It would work as long as it's a managed DLL, made with any .NET language. 6a)它可以工作,只要它是一个托管DLL,用任何.NET语言编写。

  • What is DLL importing? 什么是DLL导入? and dll registration? 和DLL注册?

"DLL importing" can mean many things, usually it means referencing a DLL file and using things in it. “DLL导入”可能意味着很多东西,通常意味着引用DLL文件并使用其中的内容。

DLL registration is something that's done on Windows to globally register DLL files as COM components to make them available to any software on the system. DLL注册是在Windows上完成的,它将DLL文件全局注册为COM组件,以使它们可供系统上的任何软件使用。

A .dll file contains compiled code you can use in your application. .dll文件包含可在应用程序中使用的已编译代码。

Sometimes the tool used to compile the .dll matters, sometimes not. 有时,用于编译.dll的工具很重要,有时则不然。 If you can reference the .dll in your project, it doesn't matter which tool was used to code the .dll's exposed functions. 如果您可以在项目中引用.dll,那么使用哪个工具来编写.dll的公开函数并不重要。

The linking happens at runtime, unlike statically linked libraries, such as your classes, which link at compile-time. 链接发生在运行时,不像静态链接库,例如您的类,它们在编译时链接。

You can think of a .dll as a black box that provides something your application needs that you don't want to write yourself. 您可以将.dll视为一个黑盒子,它提供您不希望自己编写的应用程序所需的内容。 Yes, someone understanding the .dll's signature could create another .dll file with different code inside it and your calling application couldn't know the difference. 是的,有人理解.dll的签名可能会创建另一个包含不同代码的.dll文件,并且您的调用应用程序无法知道其中的差异。

HTH HTH

1) At what point does web.dll dynamically link to business.dll? 1)web.dll在什么时候动态链接到business.dll? You notice a lot in Windows HDD thrashing for seemingly small tasks when using Word etc and I reckon that this Word going off and dynamically linking in functionality from other DLL's? 在使用Word等时,你会注意到Windows硬盘驱动器中有很多看似小的任务,我认为这个Word会在其他DLL的功能中动态链接并动态链接?

1) I think you are confusing linking with loading. 1)我认为你混淆加载链接。 The link is when all the checks and balances are tested to be sure that what is asked for is available. 链接是在测试所有检查和平衡时确保所要求的内容可用。 At load time, parts of the dll are loaded into memory or swapped out to the pagefile. 在加载时,部分dll被加载到内存中或换出到页面文件。 This is the HD activity you are seeing. 这是您看到的高清活动。

Dynamic linking is different from static linking in that in static linking, all the object code is put into the main .exe at link time. 动态链接与静态链接的不同之处在于,在静态链接中,所有目标代码都在链接时放入主.exe中。 With dynamic linking, the object code is put into a separate file (the dll) and it is loaded at a different time from the .exe. 通过动态链接,目标代码被放入一个单独的文件(dll)中,并在与.exe不同的时间加载。

Dynamic linking can be implicit (ie the app links with a import lib), or explicit (ie the app uses LoadLibrary(ex) to load the dll). 动态链接可以是隐式的(即app链接到import lib),也可以是显式的(即app使用LoadLibrary(ex)加载dll)。

In the implicit case, /DELAYLOAD can be used to postpone the loading of the dll until the app actually needs it. 在隐式的情况下,/ DELAYLOAD可用于推迟加载dll,直到应用程序实际需要它为止。 Otherwise, at least some parts of it are loaded (mapped into the process address space) as part of the process initilazation. 否则,作为进程初始化的一部分,至少将其一部分加载(映射到进程地址空间)。 The dll can also request that it never be unloaded while the process is active. dll还可以请求在进程处于活动状态时永远不会卸载它。

COM uses LoadLibrary to load COM dlls. COM使用LoadLibrary加载COM dll。 Note that even in the implicit case, the system is using something similar to LoadLibrary to load the dll either at process startup or on first use. 请注意,即使在隐式情况下,系统也会使用与LoadLibrary类似的内容在进程启动时或首次使用时加载dll。

2) What actually executes the code in the DLL? 2)实际执行DLL中的代码是什么? Does it get executed by the processor or is there another stage of translation or compilation before the processor will understand the code inside the DLL? 它是由处理器执行还是在处理器理解DLL中的代码之前还有另一个转换或编译阶段?

2) Dlls contain object code just like .exes. 2)Dll包含与.exes类似的对象代码。 The format of the dll file is almost identical to the format of an exe file. dll文件的格式几乎与exe文件的格式相同。 I have heard that there is only one bit that is different in the headers of the two files. 我听说在两个文件的标题中只有一个不同的位。

In the case of a DLL built from C# .net, the .Net framework is running it. 对于使用C#.net构建的DLL,.Net框架正在运行它。

3) Does a DLL from say Linux work on a Windows system (if such a thing exists) or are they operating system specific? 3)说Linux的DLL是否适用于Windows系统(如果存在这样的东西)或者它们是否特定于操作系统?

3) DLLs are platform specific. 3)DLL是特定于平台的。

4) Are they specific to a particular framework? 4)它们是否特定于特定框架? Can a DLL built using C# .Net be used by a DLL built with Borland C++ (example only)? 使用C#.Net构建的DLL是否可以由使用Borland C ++构建的DLL(仅限示例)使用?

4) Dlls can interoperate with other frameworks if special care is taken or some additional glue code is written. 4)如果采取特殊措施或编写一些额外的胶水代码,Dll可以与其他框架互操作。

Dlls are very useful when a company sells multiple products that have overlapping capabilities. 当公司销售具有重叠功能的多个产品时,Dll非常有用。 For instance, I maintain a raster i/o dll that is used by more than 30 different products at the company. 例如,我维护一个光栅i / o dll,该公司使用了30多种不同的产品。 If you have multiple products installed, one upgrade of the dll can upgrade all the products to new raster formats. 如果您安装了多个产品,则dll的一次升级可以将所有产品升级为新的栅格格式。

5) Going back to the web.dll / business.dll example. 5)回到web.dll / business.dll示例。 To get a class type of customer I need to reference business.dll from web.dll. 要获得类类型的客户,我需要从web.dll引用business.dll。 This must mean that business.dll contains a specification of some sort of what a customer class actually is. 这必须意味着business.dll包含某种客户类实际上的规范。 If I had compiled my business.dll file in say Delphi would C# understand it and be able to create a customer class - or is there some sort of header info or something that says "hey sorry you can only use me from another delphi dll". 如果我已经编译了我的business.dll文件,比如说Delphi会让C#理解它并能够创建一个客户类 - 或者是否有某种标题信息或者说“嘿抱歉你只能用我的另一个delphi dll” 。

5) Depending on the platform, the capabilities of a dll are presented in various ways, thru .h files, .tlb files, or other ways on .net. 5)根据平台的不同,dll的功能以各种方式呈现,通过.h文件,.tlb文件或.net上的其他方式。

6) On the subject of DLL hijacking, surely the replacement (bad) DLL must contain the exact method signatures, types as the one that is being hijacked. 6)关于DLL劫持的主题,当然,替换(坏)DLL必须包含确切的方法签名,类型是被劫持的类型。 I suppose this wouldnt be hard to do if you could find out what methods etc were available in the original DLL. 我想如果你能找到原始DLL中可用的方法等,那么这很难做到。

6) dumpbin /exports and dumbin /imports are interesting tools to use on .exe and .dlls 6)dumpbin / exports和dumbin / imports是在.exe和.dll上使用的有趣工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM