Is there a complete user32.dll wrapper library available for .NET?
Solution 1
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:
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.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.
Solution 2
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
pimvdb
Updated on June 04, 2022Comments
-
pimvdb almost 2 years
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.
-
pimvdb over 13 yearsI see, thanks. Am I right though in thinking that there is no plain wrapper DLL?
-
Cody Gray over 13 years@Downvoter: Care to leave a comment on what part of my answer you think is incorrect?
-
Uwe Keim over 13 yearsAt least I never saw one. Probably it would get rather huge and make your application unnecessarily large. I just know that the mshtml wrapper is around 7MB and that is just for one app. Another reason might be that different OS have different (sub)sets of functions in the user32.dll.
-
pimvdb over 13 yearsFirst of all, thanks for your clear explanation. I see your point, but not all of user32.dll's functions are in the .NET Framework. I think I'll go ahead just creating my own simple wrapper with the functions which need interopping I use most. Lastly, however, I do not see why the DllImport attribute might be benificial over the Declare statement. I guess VB.NET has its separate keyword Declare for such things, which is also clearer in my opinion than the notation you use.
-
Cody Gray over 13 years@pimvdb: I understand the need to P/Invoke certain functions. I have a large wrapper library for advanced things like low-level system hooks. I just noticed in your question that you were looking for a complete wrapper, and I think that would be a mistake, considering how many functions have already been implemented in managed code for you by the .NET Framework. And I've updated my answer with a couple of reasons why I recommend
<DllImport>
overDeclare
. -
pimvdb over 13 yearsI'm sorry for not being clear, but basically I was looking for a way to prevent all the copy-and-pasting I was constantly doing. Anyway, you've been extremely helpful, thanks very much.