什么是DLL,它是如何工作的? [英] What is in a DLL and how does it work?

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

问题描述

我总是在C#代码中引用DLL,但它们仍然是一个谜,我想澄清一下。这是一个有关DLL的问题的脑转储。



我理解一个DLL是一个动态链接的库,这意味着另一个程序可以在运行时访问这个库,以获得功能性。但是,请考虑以下ASP.NET项目与 Web.dll Business.dll Web.dll 是前端功能,它为类型和方法引用 Business.dll


  1. Web.dll 动态链接到 Business.dll ?在Windows中,您会注意到使用Word(等)时看起来很小的任务的Windows硬盘,并且我认为Word正在关闭,并从其他DLL动态链接功能。



    1a上。此外,什么加载和链接DLL - 操作系统或一些运行时框架,如.NET框架?



    1b。 连接的过程是什么?是否进行兼容性检查?加载到同一个内存中?链接实际上是什么意思?


  2. 什么实际执行DLL中的代码?处理器是否可以由处理器执行,还是在处理器将理解DLL内的代码之前还有另一个阶段的翻译或编译?



    2a。在C#.NET中构建DLL的情况下,运行这个方法是什么:.NET框架或操作系统?


  3. 在Windows系统上工作(如果这样的事情存在),或者它们是否具体操作系统?


  4. DLL是否特定于特定的框架?使用C#.NET构建的DLL可以使用例如Borland C ++构建的DLL来使用吗?



    4a。如果4的答案是否,那么DLL的意义是什么?为什么不用各种框架使用自己的链接文件格式?例如:.NET中构建的.exe知道.abc的文件类型可以链接到其代码中。


  5. 回到 Web.dll / Business.dll 示例 - 获取一类需要引用的客户类型 Business.dll Web.dll 。这必须意味着 Business.dll 包含一些关于客户类实际是什么的规范。如果我已经编译了我的 Business.dll 文件,比如说Delphi:C#可以理解它,并且能够创建一个客户类,或者有某种头信息或嘿,抱歉你只能使用我从另一个Delphi DLL的东西?



    5a。同样适用于方法;我可以在DLL中编写一个 CreateInvoice()方法,用C ++编译,然后从C#访问并运行它?什么阻止或允许我这样做?


  6. 关于DLL劫持的主题,确定替换(坏)DLL必须包含确切的方法签名和类型正在被劫持的那个。我想这不是很难做,如果你可以找到什么方法可用在原始的DLL。



    6a。我的C#程序在决定是否可以访问另一个DLL?如果我的劫持的DLL包含与原始DLL完全相同的方法和类型,但是它是用另一种语言编译的,它会工作吗?


什么是DLL导入和DLL注册?

解决方案

这里我可以随便指出任何错误。我不完全是Windows内部的专家...



首先,您需要了解两种非常不同类型的DLL之间的区别。 Microsoft决定使用与.NET(托管代码)和本机代码相同的文件扩展名(.exe和.dll),但是托管代码DLL和本机DLL在内部是


1)web.dll在什么时候动态链接到business.dll? $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
b $ b

1)在.NET的情况下,当执行第一个尝试访问DLL中的任何东西的DLL时,DLL通常会按需加载。这就是为什么如果DLL无法加载,您可以在代码中的任何位置获取TypeNotFoundExceptions。当像Word这样的东西突然开始访问HDD时,它很可能会交换(获取已经被换出到磁盘中的数据,以便在RAM中腾出空间)


1a)另外,加载和链接DLL - O / S或一些
运行时框架,如.Net框架?


1a)在托管DLL的情况下,.NET框架是加载的,JIT编译(将.NET字节码编译为本机代码)并链接DLL。在本机DLL的情况下,它是加载和链接DLL的操作系统的组件(不需要编译是必需的,因为本机DLL已经包含本机代码)。


1b)链接的过程是什么?支票是否有
的兼容性?加载到同一个内存中?链接
实际上是什么意思?


1b)链接是在调用代码中的引用(例如方法调用)到符号(例如方法)在DLL中被替换为DLL中的事物的实际地址。这是必要的,因为DLL的最终地址在加载到内存中之前不能被知道。


2)什么实际执行DLL中的代码?处理器是否可以由
处理器执行,还是在处理器将理解DLL内的代码之前还有另一个阶段的翻译或编译


2)在Windows上,.exe文件和.dll文件完全相同。本机.exe和.dll文件包含本机代码(处理器执行的相同内容),因此无需翻译。托管的.exe和.dll文件包含.NET字节码,这是第一个JIT编译(翻译成本机代码)。


2a)在这种情况下的一个DLL从C#.net构建什么是运行这个?
.Net框架或操作系统直接?


2a)代码已经编译成JIT之后,它运行在与任何代码完全相同的方式。


3)一个DLL是否在Linux上工作在Windows系统上(如果这样的事情
存在)或者它们是否具体操作?


3)托管DLL可能按原样工作,只要框架两个平台都是最新的,谁写的DLL没有故意破坏兼容性使用本机调用。本机DLL不能正常工作,因为格式是不同的(即使内部机器代码相同,如果它们都是同一个处理器平台)。顺便说一句,在Linux上,DLL被称为.so(共享对象)文件。


4)它们是否具体到特定框架?可以使用
构建的DLL C#.Net由使用Borland C ++构建的DLL(仅适用于示例)使用?


4 )受管理的DLL是.NET框架特有的,但是它们自然可以使用任何兼容的语言。只要所有人使用相同的约定(调用约定(函数参数如何在机器代码级别传递),符号命名等),本地DLL都是兼容的。


5)回到web.dll / business.dll示例。要得到一个类
类型的客户,我需要从web.dll引用business.dll。这个
必须意味着business.dll包含一个客户类实际上是某种类型的
的规范。如果我编译了我的business.dll
文件,说Delphi会C#理解它,并能够创建一个
客户类 - 或者有某种标题信息或
表示嘿,对不起,你只能使用我从另一个delphi dll。


5)托管DLL包含对每个类,方法,字段等。 AFAIK Delphi不支持.NET,因此它将创建本机DLL,不能直接在.NET中使用。您可能可以使用PInvoke调用函数,但不会找到类定义。我不使用Delphi,所以我不知道如何使用DLL存储类型信息。例如,C ++依赖于包含类型声明的头(.h)文件,并且必须与DLL一起发布。


6)关于DLL劫持的问题,当然,替换(坏)DLL
必须包含确切的方法签名,类型是
被劫持的类型。我想这不是很难做,如果你可以找到
在原始的DLL中可用的方法等。


6)事实上,如果你可以轻松地切换DLL,这不难做到。可以使用代码签名来避免这种情况。为了让某人更换一个已签名的DLL,他们必须知道密钥保密的签名密钥。


6a)有点一个重复的问题在这里,但这可以追溯到我的
C#程序正在决定是否可以访问另一个DLL?如果我被劫持的DLL
包含与原始的方法和类型完全相同的方法和类型,那么
会被编译成另一种语言吗?


6a)它可以工作,只要它是一个托管的DLL,用任何.NET语言。



  • 什么是DLL导入?和dll注册?


DLL导入可以意味着很多东西,通常意味着引用一个DLL文件并使用其中的东西。



DLL注册是在Windows上完成的,将DLL文件全局注册为COM组件,使其可用于系统上的任何软件。 / p>

I'm always referencing DLLs in my C# code, but they have remained somewhat of a mystery which I would like to clarify. This is a sort of brain dump of questions regarding DLLs.

I understand a DLL is a dynamically linked library which means that another program can access this library at run time to get "functionality". 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).

  1. At what point does Web.dll dynamically link to 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?

    1a. Additionally, what loads and links the DLL - the OS or some run time framework such as the .NET framework?

    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? 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?

    2a. In the case of a DLL built in C# .NET, what is running this: the .NET framework or the operating system directly?

  3. Does a DLL from Linux work on a Windows system (if such a thing exists), or are they operating system specific?

  4. Are DLLs specific to a particular framework? Can a DLL built using C# .NET be used by a DLL built with, for example, Borland C++?

    4a. If the answer to 4 is "no" then what is the point of a 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.

  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. This must mean that Business.dll contains some sort of a specification as to what a customer class actually is. 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"?

    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#? 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. I suppose this wouldn't be hard to do if you could find out what methods were available in the original DLL.

    6a. What in my C# program is deciding if I can access another 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?

What is DLL importing and DLL registration?

解决方案

Here I go, feel free to point out any errors. I'm not exactly an expert with Windows internals...

First of all, you need to understand the difference between two very different kinds of DLLs. 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.

1) At what point does web.dll dynamically link to 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?

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. This is why you can get TypeNotFoundExceptions anywhere in your code if a DLL can't be loaded. 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)

1a) Additionally what loads and links the DLL - the O/S or some runtime framework such as the .Net framework?

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. 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).

1b) What is the process of "linking"? Are checks made that there is compatibility? Loading into the same memory? What does linking actually mean?

1b) Linking is when references (e.g. method calls) in the calling code to symbols (e.g. methods) in the DLL are replaced with the actual addresses of the things in the DLL. This is necessary because the eventual addresses of the things in the DLL cannot be known before it's been loaded into memory.

2) What actually executes the code in the 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?

2) On Windows, .exe files and .dll files are quite identical. Native .exe and .dll files contain native code (the same stuff the processor executes), so there's no need to translate. Managed .exe and .dll files contain .NET bytecode which is first JIT compiled (translated into native code).

2a) In the case of a DLL built from C# .net what is running this? The .Net framework or the operating system directly?

2a) After the code has been JIT compiled, it's ran in the exact same way as any code.

3) Does a DLL from say Linux work on a Windows system (if such a thing exists) or are they operating system specific?

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. 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). By the way, on Linux, "DLLs" are known as .so (shared object) files.

4) Are they specific to a particular framework? Can a DLL built using C# .Net be used by a DLL built with Borland C++ (example only)?

4) Managed DLLs are particular to the .NET framework, but naturally they work with any compatible language. 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)

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. This must mean that business.dll contains a specification of some sort of what a customer class actually is. 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".

5) Managed DLLs contain a full description of every class, method, field, etc they contain. AFAIK Delphi doesn't support .NET, so it would create native DLLs, which can't be used in .NET straightforwadly. You will probably be able to call functions with PInvoke, but class definitions will not be found. I don't use Delphi so I don't know how it stores type information with DLLs. C++, for example, relies on header (.h) files which contain the type declarations and must be distributed with the 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. I suppose this wouldnt be hard to do if you could find out what methods etc were available in the original DLL.

6) Indeed, it's not hard to do if you can easily switch the 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.

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? If my hijacked DLL contained exactly the same methods and types as the original but it was compiled in another lanugage would it work?

6a) It would work as long as it's a managed DLL, made with any .NET language.

  • What is DLL importing? and dll registration?

"DLL importing" can mean many things, usually it means referencing a DLL file and using things in it.

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,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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