windows 是否有可用于 .NET 的完整 user32.dll 包装库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4572349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a complete user32.dll wrapper library available for .NET?
提问by pimvdb
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.
我目前正在通过 VB.NET 对 user32.dll 进行大量互操作。由于 user32.dll 不是在 .NET 级别而是在本机级别,我需要使用 Declare 语句声明函数。虽然这很有效,但我一直一遍又一遍地声明它们。
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.
我在谷歌上搜索,但我遇到的唯一有用的网站是pinvoke.net。虽然它在一定程度上确实包含信息,但有相当多的功能要么没有描述,要么在它的文档中包含很多“待办事项”部分。
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.
因此,我想知道是否有可用于 .NET 的 user32.dll 的完整包装器。恐怕我找不到任何东西,但也许我没有找到正确的方法。
采纳答案by Cody Gray
A completewrapper 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.
.NET Framework 的 user32.dll的完整包装器将毫无意义。user32.dll导出的绝大多数函数都有.NET Framework原生实现的对应函数。事实上,整个 .NET Framework 只是 Windows API 的一个包装器,包括 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.
我建议您不要尝试从 user32.dll 执行 P/Invoke 函数,因为有一种方法可以使用 .NET Framework 已经提供的功能通过托管代码执行此操作。在尝试自己重新发明轮子之前,首先检查 MSDN 或您选择的方便的 .NET 参考指南。
If and when you determine that the specific function(s) you need does nothave 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.
如果并且当您确定您需要的特定功能没有本机等效项时,那么并且只有在那时您才应该考虑 P/Invoking Windows API。在这种情况下,由于您已经大大减少了必须导入的函数数量,因此使用 MSDN 文档pinvoke.net的组合来确定函数签名应该只需要很少的工作,和堆栈溢出。我想说自己编写这段代码的好处(现在您已经将需要的内容缩减到更易于管理的大小)是您实际上需要阅读文档并准确了解它是如何工作的。如果您依赖其他人编写的代码,则无法保证其编写正确、遵循最佳实践、实现了任何错误处理,甚至无法保证您了解它的工作原理和使用方法。
Finally, I recommend that even in VB.NET, you use the standard C# syntax for P/Invoking functions, rather than Declare
. For example:
最后,我建议即使在 VB.NET 中,您也对 P/Invoking 函数使用标准的 C# 语法,而不是Declare
. 例如:
<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.
该
Declare
语法试图保持与 VB 6 处事方式的向后兼容性。官方的 .NET 方式(在 C# 中常用)是使用<DllImport>
属性,并且由于您正在编写面向 .NET Framework 的全新代码,因此您应该强烈考虑使用官方语法。这里的另一个好处是,使用 C# 的人会更熟悉您的代码,并且他们将更能够帮助您进行声明。您在网上找到的示例很可能是以这种方式编写的,而不是使用传统的 VB 6 样式语法。有时,
Declare
语法有一些意想不到的行为。例如,某些类型的编组方式与标准<DllImport>
语法不同。对于更熟悉标准 .NET 行为的人来说,这可能会令人困惑。
Also see this questionaddressing a similar issue.
另请参阅解决类似问题的此问题。
回答by cjbarth
I would actually just use the pre-built wrapper called Managed Windows API. It doesn't have everything, but it has most things. When used in connection with the PInvoke.net Visual Studio Add-inyou should have a pretty automatic way of dealing with this problem.
我实际上只是使用名为Managed Windows API的预构建包装器。它没有一切,但它拥有大多数东西。当与PInvoke.net Visual Studio Add-in 一起使用时,您应该有一种非常自动的方法来处理这个问题。
回答by Uwe Keim
There are several automatic P/Invoke signature generatorswhich may help you.
有几个自动P/Invoke 签名生成器可以帮助您。