为什么 HRESULT 0 表示成功? [英] Why is HRESULT 0 for success?

查看:51
本文介绍了为什么 HRESULT 0 表示成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理 HRESULT 返回值时遇到了一些非常尴尬的事情,似乎成功为 0,失败为 1.这背后的逻辑是什么?

I just ran into something very awkward while working with HRESULT return values, it seems that success is 0 and failure is 1. What is the logic behind this?

我实际上尝试了 if(!hr) 并惨遭失败,浪费了我一个小时的生命,直到我发现实际成功 retval 为 0.我想打电话给想到这一点的人一个白痴,但我会尽量冷静下来 - 希望有人能对这个约定有所了解.

I actually tried if(!hr) and failed miserably, wasting an hour of my life until I figured out the actual success retval is 0. I would like to call the person who thought of this an idiot, but I'll try to cool down - hoping someone will shed some light on this convention.

推荐答案

HRESULTs 的最初目的是正式列出公共和 Microsoft 内部使用的错误代码范围,以防止不同子系统中的错误代码之间发生冲突.OS/2 操作系统.

The original purpose of HRESULTs was to formally lay out ranges of error codes for both public and Microsoft internal use in order to prevent collisions between error codes in different subsystems of the OS/2 Operating System.

这就是为什么,值为 0(HRESULT 的最高位)表示没有错误",即成功.

That's why, value of 0 (of highest bit of HRESULT) means 'no error', that is, success.

但是应该小心,因为 HRESULT 有可能将最高位设置为 0 而其他位设置为不同于 0.这意味着检查 if(0 == hr) { ... } 可能不适用于某些成功案例.检查成功的正确方法是 if(0 <= hr) { ... }.

But one should be careful, since it is possible for HRESULT to have highest bit set to 0 and other bits set different from 0. That means, the check if(0 == hr) { ... } may not work for some successful cases. The correct way to check for success is if(0 <= hr) { ... }.

维基百科有更多详细信息.

成功FAILED 宏可用于避免在处理 HRESULT 值时出错.if(0 <= hr) { ... }if(SUCCEEDED(hr)) { ... } 基本上是一样的东西.

SUCCEEDED and FAILED macros can be used to avoid mistakes when dealing with HRESULT values. if(0 <= hr) { ... } and if(SUCCEEDED(hr)) { ... } are basically the same things.

这篇关于为什么 HRESULT 0 表示成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆