Methods tagged extern with [DllImport] Typically, attributes are called C libraries. This function is useful for invoking WinAPI or legacy code.
This is an example from MSDN:
using System; using System.Runtime.InteropServices; class MainClass { [DllImport("User32.dll")] public static extern int MessageBox(int h, string m, string c, int type); static int Main() { string myString; Console.Write("Enter your message: "); myString = Console.ReadLine(); return MessageBox(0, myString, "My Message Box", 0); } }
It calls MessageBox , which is defined inside the Windows library user32.dll . Runtime does all the hard work for you here, although sometimes you need to manually manage memory. If you made a mistake in the signature, your program may fail when called, you may introduce a leak, otherwise the method may return something completely different, so be careful! I find pinvoke.net a great tool to fix signatures for different APIs.
Some extern methods inside the .NET Framework that do not have the [DllImport] attribute but are decorated with [MethodImpl (MethodImplOptions.InternalCall)] , as a rule, are implemented in the CLR itself, which is also written in C. Some of these methods simply do not can be implemented in C #, since they themselves manage the runtime itself, and some are implemented in C, because their performance is critical, and C is faster.
This is what MSDN says about them:
Specifies an internal call. An internal call is a call to a method that is implemented in the runtime of a regular language itself.
Regarding the actual implementation code, I doubt that you can get it from Microsoft, but there are some interesting alternative CLR implementations around so be sure to check them out.
Dan Abramov Feb 24 2018-11-21T00: 00Z
source share