Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm doing alot of interop to user32.dll at the moment through VB.NET. As user32.dll is not at .NET level but at native level, I need to declare the functions using the Declare statement. Although this works great, I keep declaring them over and over again.

I was Googling around but the only useful site I came across was pinvoke.net. While it does contain information to a certain extent, there are quite a number of functions either not described or with alot of "TODO" parts in the documentation of it.

Therefore, I was wondering whether there is a complete wrapper for user32.dll available for .NET. I have not been able to find any I'm afraid, but perhaps I'm not looking correctly.

like image 571
pimvdb Avatar asked Dec 31 '10 21:12

pimvdb


1 Answers

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.

like image 77
Cody Gray Avatar answered Oct 17 '22 06:10

Cody Gray