简体   繁体   English

在C#中加载C ++ dll时,CLR是否会自动加载C ++文件的依赖项?

[英]When loading a C++ dll in C#, does the CLR load the dependencies of the C++ file automatically?

Well that is my question, I am planning to implement a C++ dll which has many C++ dependencies, and I want to use it in my c# code..What I wanted to know is if the CLR loads the dependencies on its own or will I have to do something else. 好吧,这就是我的问题,我打算实现一个具有许多C ++依赖项的C ++ dll,并且我想在我的c#代码中使用它。我想知道的是,CLR是否自行加载依赖项?必须做其他事情。 When loading a C++ dll in C#, does the CLR load the dependencies of the C++ file automatically? 在C#中加载C ++ dll时,CLR是否会自动加载C ++文件的依赖项?

If its a native C++ dll, the dependencies are loaded at load time, unless that dependency is marked as delay loaded, which it will then get loaded when one of the functions from that dll is called. 如果它是本机C ++ dll,则依赖项将在加载时加载,除非该依赖项被标记为延迟加载,然后在调用该dll中的函数之一时将其加载。

If its a .Net C++ dll, dependencies are loaded when needed. 如果它是.Net C ++ dll,则在需要时加载依赖项。

Technically, the answer is no. 从技术上讲,答案是否定的。 The CLR does not load the dependencies of a native DLL -- but that's purely a technicality -- they will be loaded automatically (if possible and available). CLR不会加载本机DLL的依赖项-但这纯粹是技术性问题-它们将自动加载(如果可能且可用)。

What actually happens is slightly complex. 实际发生的情况有些复杂。 The CLR doesn't (itself) load DLLs at all -- DLLs are loaded by the native Windows loader. CLR根本不(自身)加载DLL-DLL由本机Windows加载器加载。 In the case of a .NET assembly, you have the CLR code stored as basically a binary resource. 对于.NET程序集,您将CLR代码基本上存储为二进制资源。 You also have a DllMain that invokes the native entry point to the CLR. 您还具有一个DllMain,它调用CLR的本机入口点。 When the CLR is invoked from DllMain, it initializes the assembly. 从DllMain调用CLR时,它将初始化程序集。

The normal Windows loader will also (of course) load native DLLs. 普通的Windows加载程序也将(当然)加载本机DLL。 In this case, the loader (in the absence of a delayload flag) will go through the DLLs dependencies and load them as well. 在这种情况下,加载程序(在没有delayload标志的情况下)将遍历DLL依赖项并也加载它们。 Even if the loading of the initial DLL was triggered from the CLR, the CLR will have no further involvement until the DLL and all its implicit dependencies have been loaded. 即使从CLR触发了初始DLL的加载,在DLL及其所有隐式依赖项都已加载之前,CLR也不会进一步参与。 Only after all those DLLs (could also include things like fonts, executables, etc.) are "loaded" will control return to the CLR at all. 只有所有这些DLL(还可能包括字体,可执行文件等)被“加载”之后,才完全控制返回CLR。

Note I put "loaded" in quotes there for a reason -- the DLL won't normally be loaded into memory either -- the loader will just set up page tables to map it to memory, and then those pages will be loaded on demand (though some pages, such as those holding that DLL's DllMain will be loaded more or less immediately, when its DllMain is invoked). 请注意,由于某种原因,我将“已加载”放在引号中是有原因的-DLL通常也不会加载到内存中-加载器只会设置页面表以将其映射到内存,然后将按需加载这些页面(虽然有些页面,比如那些持有 DLL的DllMain将或多或少立即加载,调用它的DllMain时)。

So assuming you have a .NET assembly that uses a native DLL with native dependencies, the sequence would be: 因此,假设您有一个使用具有本地依赖项的本地DLL的.NET程序集,则顺序为:

  1. Load assembly 负载组装
  2. assembly's DllMain invokes CLR 程序集的DllMain调用CLR
  3. CLR loads native DLL CLR加载本机DLL
  4. Loader loads all dependencies of the native DLL 加载程序加载本机DLL的所有依赖项
  5. Only after all the implicit dependencies are loaded, control returns to CLR. 仅在加载所有隐式依赖项之后,控制权才返回到CLR。

是的,但前提是它们在搜索路径(例如,应用程序路径,Windows系统路径等)中可用

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

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