Platform Invoke

Platform Invoke—PInvoke—provides a means for C# programs to execute native code. This is of great help when there's a need to reuse legacy code or communicate with systems that don't have other readily available interfaces. Once legacy code is wrapped in a DLL, it can be called with C# through PInvoke.

Another use of PInvoke is to access existing operating system and third-party DLLs. Using PInvoke is as simple as declaring the method prototype as static extern and decorating it with the DllImport attribute. Listing 30.9 has a couple examples of how to use the DllImport attribute to implement PInvoke.

Listing 30.9. Platform Invoke Demo: PinvokeDemo.cs
 using System; using System.Runtime.InteropServices; /// <summary> /// Platform ...

Get C# Unleashed 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.