June 2, 2011, 7:01 a.m.
posted by fullstack
Use P/Invoke with Ease
P/Invoke can be extremely tricky. With the help of a wiki and an add-in, you can make it as easy as a cut and paste.
The .NET Framework includes a large amount of functionality, but it does not include everything. For a lot of different functions, you will find yourself using something called P/Invoke, which is short for platform invoke. P/Invoke allows you to call Win32 or other unmanaged APIs from your managed code, but it is not the easiest thing to do. To make a P/Invoke call, you need to create the correct signature, invoke the method, and hope that all goes well. Because you are working with an unmanaged API, this process is much more susceptible to error. This is where PInvoke.NET comes into play.
Using PInvoke.NET, you can look for, and usually find, the P/Invoke signature call that you are looking for. The PInvoke.NET main screen is shown in Figure.
As an example, let's look for the signature for the FindWindow API.
If you knew that FindWindow was located in user32.dll, you could browse using the tree on the left of the screen. Or you could simply enter the term FindWindow in the Search box and the first result would be the page shown in Figure.
PInvoke.NET signature page
This page contains a number of details about the FindWindow API. The first item on the page is the C# signature needed to call this API from C#. The page also includes notes on the API as well as a tip on how to find the name for the specific window you might be using. The page also includes sample code that you could cut and paste directly into Visual Studio.
Using the Add-in
The PInvoke.NET wiki becomes even more valuable when you take advantage of the Visual Studio add-in. The first step is to download and install the add-in, which can done by clicking on the "Get the Visual Studio add-in" link at the top of the PInvoke.NET page or by going to the following link:
Once you have installed the add-in, you can use it by simply right-clicking on your document and selecting Insert PInvoke Signatures from the right-click menu, which is shown in Figure.
PInvoke add-in menu
If you select Insert PInvoke Signatures from the menu, the next dialog you will see is shown in Figure.
Insert PInvoke Signatures dialog
Using this dialog, you can search for any function. In this example, I am going to search for the FindWindow function. The results of that search are shown in Figure.
Function search results
As you can see in Figure, the add-in found a matching signature and has selected it for you. You simply need to click the Insert button, and the following code will be inserted into your document:
[DllImport("user32.dll")] static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
You can also use the add-in to contribute new signatures to the wiki. This can be done by selecting the "Contribute PInvoke signatures and types" item in the right-click menu. You will then see the dialog shown in Figure.
"Contribute PInvoke signatures and types" dialog
Using this dialog, you can enter signatures and types, and they will be uploaded to PInvoke.NET. To add notes or code samples, you will need to visit the site in your browser.
PInvoke.NET and its companion add-in were developed by Adam Nathan whose blog can be found at http://blogs.msdn.com/adam_nathan. Both the wiki and add-in are extremely useful tools for working with one of the most error-prone and frustrating parts of .NET development.