How does extern work in c++?

extern is with platform invocation (pinvoke) to facilitate managed assemblies calling into unmanaged code. The extern keyword informs the compiler that it will need to generate the correct code for allow for the correct data marshaling.


Methods marked extern with [DllImport] attribute are usually calls to C libraries. This feature is useful for calling WinAPI or legacy code.

This is 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 Windows user32.dll library. Runtime does all the heavy work for you here, although sometimes you'd need to manually manage memory. If you get the signature wrong, your program may fail on the call, you may introduce a leak or the method might return something completely different, so be careful! I find pinvoke.net a great instrument to correct signatures for different APIs.

Some extern methods inside .NET Framework that don't have have [DllImport] attribute but are decorated with [MethodImpl (MethodImplOptions.InternalCall)] attribute are usually the ones implemented in CLR itself, which is written in C as well. Some of such methods just can't be implemented in C# because they manage 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 within the common language runtime itself.

As for looking at the actual implementation code, I doubt you'll be able to get it from Microsoft but there are some cool alternative implementations of CLR around so be sure to check them out.


Consider reading section 10.6.7 of the C# specification, which answers many of your questions. I reproduce part of it here for your convenience:


When a method declaration includes an extern modifier, that method is said to be an external method. External methods are implemented externally, typically using a language other than C#. Because an external method declaration provides no actual implementation, the method-body of an external method simply consists of a semicolon. An external method may not be generic. The extern modifier is typically used in conjunction with a DllImport attribute, allowing external methods to be implemented by DLLs (Dynamic Link Libraries). The execution environment may support other mechanisms whereby implementations of external methods can be provided. When an external method includes a DllImport attribute, the method declaration must also include a static modifier.


How does someone leverage the extern attribute?

  • Write your code in the unmanaged language of your choice.
  • Compile it into a DLL, exporting the entry point of your code.
  • Make an interop library that defines the method as an extern method in the given DLL.
  • Call it from C#.
  • Profit!

How would I go about looking into the source of extern methods like Object.InternalGetEquals()?

Go to https://github.com/dotnet/coreclr/tree/master/src/vm

Tags:

C++

Syntax

Extern