SoFunction
Updated on 2025-03-07

Detailed explanation of the difference between managed DLL and unmanaged DLL in C#

First, let’s explain the difference between a managed DLL and an unmanaged DLL. A narrow explanation,The DLL file generated by the hosted DLL is in the Dotnet environment. Unmanaged DLLs are not DLL files generated in Dotnet environments.

To host DLL files, you can directly add managed DLL files to your project by "Add Reference" in the Dotnet environment. Then use DLL to
namespace to call the corresponding DLL object.

Non-custodialDLL files, when applied in Dotnet environment,DllImport Called.

C# calls unmanaged DLL files. DLL files are written in C language.

A managed DLL is a file that can be directly referenced in the Common Language Runtime (CLR) and has the extension "DLL".

Specifically, it encapsulates the DLL files where various namespaces are located, such as, etc. Unmanaged DLLs are the usual dynamic link libraries, etc., which include DLL files that encapsulate all Windows API functions. Functions in various unmanaged DLLs cannot be called directly in the common language runtime library, but need to go through the "platform call" service provided by the .Net framework.

"Platform Call" is a service provided by the .Net framework for Visual Basic .Net, Visual C# and other .Net development languages, to introduce various types of in-hosted code
Functions encapsulated in unmanaged DLLs (including Windows API functions). "Platform Calls" rely on metadata to find export functions at runtime and encapsulate their parameters.

When using the Platform Call service in managed code to call a function encapsulated in an unmanaged DLL, Platform Services will perform the following operations in turn:

1. Find the DLL file containing the function.

2. If found, the DLL file is loaded into memory.

3. Find the address of the function in memory and push its parameters onto the stack and marshall the required data.

4. Transfer control to unmanaged functions. This way the entire function call is completed.

Using the "platform call" service in Visual Basic .Net, it states that there are two specific implementation methods for Windows API functions:

1. Use the DllImport feature class to declare Windows API functions.

2. Use the "Declare" statement to declare Windows API functions.

Although these two methods have the same effect, they are very different in traditional and simple ways. The first method declares the process is relatively complicated and it is easy to make errors when declaring Windows API functions, so it is not recommended. The second method is relatively simple and saves many syntaxes in Visual Basic before, so this method is mostly used in normal times.
to declare Windows API functions.

regasm:
Regasm registers the dll generated under the .net framework (hosted and clr)

regsvr32:
Regsvr32 registered is written in C++ (unmanaged)
For C#, you must register with Regasm

To put it bluntly, a managed DLL is a DLL that is completely implemented by .NET managed code, and it depends entirely on the CLR running of the .NET platform. Hosted DLLs are controlled by .NET CLR, support automatic memory recycling, etc., and are secure DLLs for .NET platforms

Unmanaged DLL refers to a complete or partial implementation of .NET code and can be run without relying on the .NET platform. For example, a DLL in the COM method does not support automatic memory recycling. It is also non-secure and controllable for .NET platforms.

DLL files written in languages ​​such as vc++ and vb. If used in the .net environment, you can register with regsvr32 and then use it under .net.

This is the end of this article about the difference between hosted DLL and unmanaged DLL in C#. For more related contents of C# hosted DLL and unmanaged DLL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!