零是有效的句柄吗?
.NET Framework 中有一个 SafeHandleZeroOrMinusOneIsInvalid
类,以及一个 SafeHandleMinusOneIsInvalid
类。
这是为什么呢?在哪些情况下零是有效句柄?
There is a SafeHandleZeroOrMinusOneIsInvalid
class in the .NET Framework, as well as a SafeHandleMinusOneIsInvalid
class.
Why is this? In which situations is zero ever a valid handle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为其他答案的补充讲座,请参阅此 OldNewThing 博客条目关于不一致的句柄返回值。
As additional lecture to the other answers, see this OldNewThing blog entry about inconsistent handle return values.
正如 Microsoft 在其文档中提出的(并在 Joshua,)它取决于实现,可以这么说:
As put forth by Microsoft in their documentation (and demonstrated in description by Joshua,) it is implementation dependant, so to speak:
我认为您对这个名称的解读太多了:这意味着某些 API 按照惯例返回 0 表示失败,而其他 API 则返回 -1。对于返回 -1 的 API,这并不意味着 0 将永远是有效句柄,只是 API 返回 -1 来指示失败。
因此,这实际上是 API 通常用来指示失败的值;它没有说明任何其他句柄值对于任何给定的 API 集是否有效。
I think you're reading too much into the name: all this means is that some APIs by convention return 0 to indicate failure, others return -1. For an API that returns -1, this doesn't mean that 0 will ever be a valid handle, just that the API returns -1 to indicate failure.
So this is really about the value that is typically used by an API to indicate failure; it doesn't say anything about whether any other handle values are valid or not for any given set of APIs.