Reputation: 2224
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. When loading a C++ dll in C#, does the CLR load the dependencies of the C++ file automatically?
Upvotes: 1
Views: 903
Reputation: 490148
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).
What actually happens is slightly complex. The CLR doesn't (itself) load DLLs at all -- DLLs are loaded by the native Windows loader. In the case of a .NET assembly, you have the CLR code stored as basically a binary resource. You also have a DllMain that invokes the native entry point to the CLR. When the CLR is invoked from DllMain, it initializes the assembly.
The normal Windows loader will also (of course) load native DLLs. In this case, the loader (in the absence of a delayload flag) will go through the DLLs dependencies and load them as well. 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. Only after all those DLLs (could also include things like fonts, executables, etc.) are "loaded" will control return to the CLR at all.
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).
So assuming you have a .NET assembly that uses a native DLL with native dependencies, the sequence would be:
Upvotes: 2
Reputation: 8706
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.
If its a .Net C++ dll, dependencies are loaded when needed.
Upvotes: 4
Reputation: 55720
Yes, but only if they are available in the search paths (i.e. application path, windows system path etc.)
Upvotes: 0