ASP.NET Tips: Finding the layout of functions and structs for pinvoke

So when you start working with .NET a lot, you will find out quickly that there are some things that you just can't do in .NET and you will need to drop back down to the Win32 API to do something.  When you need to do that, you pinvoke the function that you need to call, setting up the various parameters to pass to it.  This can get very tricky to setup, while some things are easy to transfer, strings for example, others are much harder.

Fortunately, there is a solution.  https://www.pinvoke.net/.  This site does a great job of listing virtually any command you may want to call, you can find them by which DLL they are in, or searching for it.  It will then give you the signature of what it should look like.  For example, if you want to use the OSVERSIONINFO struct:

pinvoke

You will notice that the site gives both C# and VB code examples.  It also sometimes gives a code example of how to use the structures or functions.  This is a great resource for anyone that needs to call the Win32 API.  There is even an add-in for Visual Studio 2003 and 2005 that you can install.  Download the PInvoke.net Add-in here

kick it on DotNetKicks.com