Managed Code to Native DLLs
The common language runtime provides a service that enables managed code to call unmanaged functions implemented in DLLs. This service is known as platform invoke, or PInvoke for short.
To understand how PInvoke works, let's start with making a call to MessageBox, a Win32 API that is implemented in user32.dll. Making calls to Win32 APIs is the most likely scenario you will run into. In the first version of the .NET runtime, many of the Win32 features have not been provided in the .NET Framework Class Libraries, requiring you to obtain the desired feature by invoking the Win32 API from your managed code.
Note that the .NET Framework already defines a MessageBox class under System.Windows.Forms namespace. You would never ...
Get .NET Programming: A Practical Guide Using C# 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.