Using P/Invoke to call low-level APIs
We have established that .NET gives you many tools to develop something quickly. It also helps you out by shielding you from the low-level details of the underlying operating system. But it also allows you to use low-level APIs if you need to.
But how can we access those APIs? The answer is Platform Invocation, or (P/Invoke). We can use this tool to access the Win32 API directly. P/Invoke bridges the gap between the two platforms so that we can mix and match to our hearts’ content.
Note
Win32 is the name of the SDK and the APIs made available. There is no such thing as a Win64 API. Our code is compiled against 64-bit Windows if you run that platform, yet we (and Microsoft) still call it the Win32 API.
How does P/Invoke work?
P/Invoke involves a couple of steps. These are the steps you must follow to use a Win32 API in a .NET application:
- Find the API you want to use.
- Find the DLL the API resides in.
- Load that...