A complete shell for user32.dll for the .NET Framework would be completely pointless. The vast majority of functions exported by user32.dll have corresponding functions that were originally implemented by the .NET Framework. In fact, the entire .NET Framework is just a wrapper around the Windows API, including user32.dll.
I recommend that you do not try to execute the P / Invoke functions from user32.dll when there is a way to do this through managed code using functions already provided by the .NET Framework. Check the MSDN or the handy .NET reference guide of your choice for this before attempting to invent the wheel yourself.
If and when you determine that certain functions (s) that you need do not have a native equivalent, then and only then you should consider the P / Call Windows API. In this case, since you have significantly narrowed the scope of functions that you must import, only minimal work should be used to determine the function signature, using a combination of MSDN documentation, pinvoke.net and a stack overflow. I would say that you yourself wrote this code (now that you have reduced what you need to a more manageable size), you need to read the documentation and understand exactly how it works. If you rely on code written by someone else, there is no guarantee that it is written correctly, that it follows best practices, that it implements any error handling, or even understands 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, not 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 several reasons why I think this is preferable:
The Declare syntax is an attempt to maintain backward compatibility with VB 6. The official .NET method (commonly used in C #) uses the <DllImport> attribute, and since you are writing new code that targets the .NET Framework, you should seriously consider using the official syntax . Another advantage here is that your code will be more familiar to people who use C #, and they will be able to help you more in your ads. Samples that you find on the Internet are likely to be written this way, instead of using the outdated VB 6 syntax.
Sometimes Declare syntax has some unexpected behavior. For example, some types are sorted differently than the standard <DllImport> syntax. This can be quite confusing for people who are more familiar with standard .NET behavior.
Also see this question on a similar issue.
Cody gray
source share