O'Reilly logo

.NET Programming: A Practical Guide Using C# by Pradeep Tapadiya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required