# Platform Invoke

C and C++ are/were often used for providing various code libraries. Examples include:

  • GTK
  • Cairo
  • SVN

Sometimes we might want to use these libraries in our projects, but we don't want to code in C/C++. This is where a technique called "Language Bindings" comes into play. Many languages/runtimes offer a way to run dynamic libraries even if they were compiled from different source languages. For example, in .NET, we can use Platform Invoke. Here's an example:

[DllImport (cairo, CallingConvention=CallingConvention.Cdecl)]
internal static extern void cairo_append_path (IntPtr cr, IntPtr path);

What Platform Invoke does is:

  1. It loads the dynamic library (DLL on Windows) into the memory
  2. It looks for the specified function
  3. It arranges arguments of that function on the stack and executes it
  4. It returns the result

Unmanaged Code

In the .NET world, it would be an example of Unmanaged Code, which is code that runs outside the runtime.

# Examples

Cairo is a good example of a library that is available only in C. CairoSharp (opens new window) is a language binding for Cairo (opens new window) in .NET C#. It allows .NET programmers to use Cairo in their projects. It has a NativeMethods (opens new window) class that exposes the C functions to the .NET world.

Another good example is GtkSharp (opens new window), which is a .NET wrapper for GTK toolkit.

# Hosting .NET in native code

There is also a way for the reversed operation - running managed .NET code withing a native C/C++ code. It's explained on MSDN (opens new window).

# References

Last Updated: 1/15/2023, 6:32:34 PM