Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the standard .NET library depend on any unmanaged DLL-s?

Tags:

.net

Just out of curiosity, does the .NET framework itself depend on any unmanaged DLL-s when accessing the standard library ? For example i call method A and -under the hood- that method A or any other method inside that method A executes a PInvoke against an unmanaged DLL ?

like image 503
Dante Avatar asked Dec 11 '25 21:12

Dante


2 Answers

Yes, the .Net libraries use unmanaged functions a lot. There are two types of unmanaged functions (that I know) the library can call: either methods from the Framework itself, or methods from some other DLL (using PInvoke).

The methods that are implemented in the framework are marked with [MethodImpl(MethodImplOptions.InternalCall)]. Those that come from other unmanaged DLLs are marked with [DllImport].

In my version of mscorlib.dll alone, there is 7241 methods that are implemented internally by the framework (e.g. the getter of string.Length) and 535 that are from some unmanaged DLL (many of them are in the internal class Win32Native).

like image 196
svick Avatar answered Dec 16 '25 14:12

svick


Yes of course, the framework contains innumerable calls to the underlying Windows API. Look at the decompiled code of File.Move:

[SecuritySafeCritical]
public static void Move(string sourceFileName, string destFileName)
{
    if (sourceFileName == null)
    {
        throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
    }
    if (destFileName == null)
    {
        throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
    }
    if (sourceFileName.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
    }
    if (destFileName.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
    }
    string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand();
    string dst = Path.GetFullPathInternal(destFileName);
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { dst }, false, false).Demand();
    if (!InternalExists(fullPathInternal))
    {
        __Error.WinIOError(2, fullPathInternal);
    }
    if (!Win32Native.MoveFile(fullPathInternal, dst))
    {
        __Error.WinIOError();
    }
}

As you can see, at the end of the game we have a call to Win32Native.MoveFile. which is defined as...

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool MoveFile(string src, string dst);
like image 44
Steve Avatar answered Dec 16 '25 12:12

Steve