c# - DLL 中有什么以及它是如何工作的?

标签 c# .net dll

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

我知道 DLL 是一个动态链接库,这意味着另一个程序可以在运行时访问该库以获得“功能”。但是,请考虑以下带有 Web.dllBusiness.dll 的 ASP.NET 项目(Web.dll 是前端功能,它引用Business.dll 用于类型和方法)。

  1. Web.dll 在什么时候动态链接到 Business.dll?您注意到,在使用 Word(等)时,Windows HDD 中出现了很多看似很小的任务,并且我认为 Word 正在关闭并动态链接其他 DLL 的功能?

    1a。此外,什么加载并链接 DLL - 操作系统或某些运行时框架(例如 .NET 框架)?

    1b。 “链接”的过程是怎样的?是否进行兼容性检查?加载到同一内存中?链接实际上意味着什么?

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

    2a。对于用 C# .NET 构建的 DLL,运行它的是:.NET 框架还是直接操作系统?

  3. Linux 中的 DLL 是否可以在 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 包含某种关于客户类别实际是什么的规范。如果我在 Delphi 中编译了我的 Business.dll 文件:C# 会理解它并能够创建客户类,或者是否有某种 header 信息或“嘿抱歉”的内容你只能从另一个 Delphi DLL 中使用我”?

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

  6. 关于 DLL 劫持的问题,替换的(坏的)DLL 肯定必须包含与被劫持的完全相同的方法签名和类型。我想如果您能找出原始 DLL 中可用的方法,这并不难。

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

什么是DLL导入和DLL注册?

最佳答案

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

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) 对于 .NET,DLL 通常在执行第一个尝试访问 DLL 中任何内容的方法时按需加载。这就是为什么如果无法加载 DLL,您可能会在代码中的任何位置获得 TypeNotFoundExceptions。当像 Word 这样的程序突然开始大量访问 HDD 时,很可能会发生交换(将已交换到磁盘的数据以在 RAM 中腾出空间)

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

1a) 对于托管 DLL,.NET 框架负责加载、JIT 编译(将 .NET 字节码编译为 native 代码)并链接 DLL。对于 native DLL,它是加载和链接 DLL 的操作系统组件(无需编译,因为 native DLL 已经包含 native 代码)。

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) 链接是指调用代码中对 DLL 中符号(例如方法)的引用(例如方法调用)被替换为 DLL 中事物的实际地址。这是必要的,因为 DLL 中的内容在加载到内存之前无法知道其最终地址。

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) 在 Windows 上,.exe 文件和 .dll 文件完全相同。 native .exe 和 .dll 文件包含 native 代码(与处理器执行的内容相同),因此无需翻译。托管 .exe 和 .dll 文件包含首先进行 JIT 编译(翻译为 native 代码)的 .NET 字节码。

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

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) 托管 DLL 可能会按原样工作,只要两个平台上的框架都是最新的,并且编写 DLL 的人没有故意通过使用 native 调用来破坏兼容性。 native DLL 不会按原样工作,因为格式不同(即使内部的机器代码相同,如果它们都用于相同的处理器平台)。顺便说一下,在 Linux 上,“DLL”被称为 .so(共享对象)文件。

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) 托管 DLL 是 .NET 框架特有的,但它们自然可以与任何兼容语言一起使用。只要每个人都使用相同的约定(调用约定(函数参数如何在机器代码级别上传递)、符号命名等), native DLL 就是兼容的

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) 托管 DLL 包含其包含的每个类、方法、字段等的完整描述。据我所知,Delphi 不支持 .NET,因此它会创建 native DLL,而这些 DLL 不能直接在 .NET 中使用。您可能能够使用 PInvoke 调用函数,但找不到类定义。我不使用 Delphi,所以我不知道它如何用 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. I suppose this wouldnt be hard to do if you could find out what methods etc were available in the original DLL.

6) 确实,如果你能轻松地切换DLL,这并不难。可以使用代码签名来避免这种情况。为了让某人替换已签名的 DLL,他们必须知道签名 key ,该 key 是保密的。

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) 只要它是使用任何 .NET 语言编写的托管 DLL,它就可以工作。

  • What is DLL importing? and dll registration?

“DLL导入”可以有很多含义,通常意味着引用DLL文件并使用其中的内容。

DLL 注册是在 Windows 上完成的,用于将 DLL 文件全局注册为 COM 组件,以便系统上的任何软件都可以使用它们。

关于c# - DLL 中有什么以及它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3628798/

相关文章:

c# - DL需要返回BL类型时如何构造一个简单的BL

c# - 序列化泛型类

c# - Linq 返回 IEnumerable<MyObject>,如何返回 MyListObject?

c - Windows 上带有 DLL 的动态模块

c++ - lib 文件有效,但 dll 文件无效

c# - 使用选择更改淡出或淡入另一个字段

c# - 枚举由 Autofac 生命周期跟踪的一次性元素

c# - 无法使用与其底层 RCW 分离的 COM 对象

c# - 如何在 C# 中停止进程,知道它的文件名?

R C 符号名称 "do_is_ordered"不在包 xts 的 DLL 中