Chapter 24. Native and COM Interoperability
This chapter describes how to integrate with native (unmanaged) Dynamic-Link Libraries (DLLs) and Component Object Model (COM) components. Unless otherwise stated, the types mentioned in this chapter exist in either the System
or the System.Runtime.InteropServices
namespace.
Calling into Native DLLs
P/Invoke, short for Platform Invocation Services, allows you to access functions, structs, and callbacks in unmanaged DLLs (shared libraries on Unix).
For example, consider the MessageBox
function, defined in the Windows DLL user32.dll, as follows:
int MessageBox (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
You can call this function directly by declaring a static method of the same name, applying the extern
keyword, and adding the DllImport
attribute:
using System; using System.Runtime.InteropServices; MessageBox (IntPtr.Zero, "Please do not press this again.", "Attention", 0); [DllImport("user32.dll")] static extern int MessageBox (IntPtr hWnd, string text, string caption, int type);
The MessageBox
classes in the System.Windows
and System.Windows.Forms
namespaces themselves call similar unmanaged methods.
Hereâs a DllImport
example for Ubuntu Linux:
Console.WriteLine ($"User ID: {getuid()}"); [DllImport("libc")] static extern uint getuid();
The CLR includes a marshaler that knows how to convert parameters and return values between .NET types and unmanaged types. In the Windows example, the int
parameters translate directly ...
Get C# 10 in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.