Is there a complete user32.dll wrapper library available for .NET?

The Vanara Project implements nearly all Win32 Windows apis. It is available at github or as nuget packages. The developers react extremely quickly to issues and requests.

The user32.dll is also completely available. Just call the Visual Studio package manager and reference to Vanara.PInvoke.User32

enter image description here


A complete wrapper for user32.dll for the .NET Framework would be pretty pointless. The vast majority of the functions exported by user32.dll have corresponding functions that are natively implemented by the .NET Framework. In fact, the entire .NET Framework is simply a wrapper around the Windows API, including user32.dll.

I recommend that you not try and P/Invoke functions from user32.dll when there is a way to do it through managed code using the functionality already provided by the .NET Framework. Check MSDN or a handy .NET reference guide of your choosing for that first, before trying to reinvent the wheel yourself.

If and when you determine that the specific function(s) you need does not have a native equivalent, then and only then should you consider P/Invoking the Windows API. In that case, since you've substantially narrowed down the amount of functions you have to import, it should be only a minimal amount of work to determine the function signature using a combination of the MSDN documentation, pinvoke.net, and Stack Overflow. I'd say the benefit of writing this code yourself (now that you've trimmed what you need down to a more manageable size) is that you're virtually required to read the documentation and understand exactly how it works. If you rely on code written by someone else, there's no guarantee that it's written correctly, that it follows best practices, that it implements any error handling, or even that you understand how it works and how to use it.

Finally, I recommend that even in VB.NET, you use the standard C# syntax for P/Invoking functions, rather than Declare. For example:

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As MenuFlags, ByVal uIDNewItem As Int32, ByVal lpNewItem As String) As Boolean
End Function

There are a couple of reasons why I think this is preferable:

  1. The Declare syntax is an attempt to maintain backwards compatibility with the VB 6 way of doing things. The official .NET way (commonly used in C#) is using the <DllImport> attribute, and since you're writing brand new code targeting the .NET Framework, you should strongly consider using the official syntax. The other benefit here is that your code will be more familiar to people who use C#, and they'll be more able to help you with your declarations. The samples you find online are most likely to be written in this way, rather than using the legacy VB 6-style syntax.

  2. Sometimes, the Declare syntax has some unexpected behavior. For example, certain types are marshalled differently than they are with the standard <DllImport> syntax. This can be quite confusing to people who are more familiar with the standard .NET behavior.

Also see this question addressing a similar issue.