3

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 ?

Dante
  • 8,751
  • 14
  • 43
  • 61

4 Answers4

3

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);
Steve
  • 203,265
  • 19
  • 210
  • 265
3

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).

svick
  • 214,528
  • 47
  • 357
  • 477
0

The framework class library uses a lot of P/Invokes against different Windows API DLLs. For example, see the internal class Microsoft.Win32.Win32Native in mscorlib which contains many DllImports. For example, System.IO.FileStream.Read ultimately uses Win32.ReadFile.

Andre Loker
  • 7,938
  • 1
  • 19
  • 35
0

If you look at the System.Runtime.CompilerServices.MethodImplOptions enum, it has a Unmanaged flag. Cant really find where it is used though.

namespace System.Runtime.CompilerServices
{
  [Flags]
  [ComVisible(true)]
  [Serializable]
  public enum MethodImplOptions
  {
    Unmanaged = 4,
    ForwardRef = 16,
    PreserveSig = 128,
    InternalCall = 4096,
    Synchronized = 32,
    NoInlining = 8,
    NoOptimization = 64,
  }
}
labroo
  • 2,629
  • 2
  • 27
  • 35