最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

E

运维笔记admin65浏览0评论

E

E

E_FAIL或S_FALSE,哪个更适合表示没有这样的属性?(E_FAIL or S_FALSE, which is more appropriate to represent no such attribute?)

我有一个图像检测模块,封装为COM模块。 我已经导出了一个Key/Value Getter API,如: GetImageAttr(UINT key, void* pValue); 。 我们的产品可能或可能不会在图像上附加特殊结构,因此我的客户可以通过此API查询特定结构。

可能的用法如下:

ImageSpecialAttribute attr = {};HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);

如果图像具有这样的附加结构,则返回S_OK是微不足道的。 但如果没有,我应该返回E_FAIL还是S_FALSE?

S_FALSE:一切都很好,只是图像没有这样的可选属性。

强制用户检查hr == S_OK 查询没有这种可选属性的图像不是错误。

E_FAIL:不! 出了点问题。 您不应该查询此密钥。

客户可以通过FAILED轻松检查(小时) 使用此密钥查询此不存在的值是一个错误。

更新,(感谢Remy Lebeau)

HRESULT_FROM_WIN32(ERROR_NOT_FOUND):不! 没有这样的元素/属性。

客户可以通过FAILED轻松检查(小时) 虽然它代表一个错误,但用户仍然可以通过检查hr来了解其含义。

I have a image detection module which is encapsulated as a COM module. I've exported a Key/Value Getter API like: GetImageAttr(UINT key, void* pValue);. Our product MAY or MAY NOT attach a special structure on image, so my client can query the specific structure through this API.

Possible usage is like:

ImageSpecialAttribute attr = {};HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);

It is trivial to return S_OK if the image does have such attached structure. But if it doesn't, should I return E_FAIL or S_FALSE?

S_FALSE: Everything is fine, just the image does not have such optional attribute.

Force user to check hr == S_OK Querying a image without such optional attribute is not a error.

E_FAIL: No! Something is wrong. You should not query this key.

Client can easily check by FAILED(hr) Using this key to query this non-existed value is a error.

Updated, (Thanks to Remy Lebeau)

HRESULT_FROM_WIN32(ERROR_NOT_FOUND): No! No such element/attribute existed.

Client can easily check by FAILED(hr) Although it represents a error, users still can know what's the meaning by checking hr. 最满意答案

S_FALSE是成功值,而不是错误值。 当方法本身成功但请求的数据不可用或未执行请求的操作时,许多Microsoft自己的COM API返回S_FALSE 。 这在Microsoft的文档中提到:

COM中的错误处理

所有带有前缀“E_”的常量都是错误代码。 常数S_OK和S_FALSE都是成功代码。 可能99%的COM方法在成功时返回S_OK; 但不要让这个事实误导你。 方法可能会返回其他成功代码,因此请始终使用SUCCEEDED或FAILED宏来测试错误。 ... 成功代码S_FALSE值得一提。 有些方法使用S_FALSE来粗略地表示非故障的负面情况。 它也可以表示“无操作” - 方法成功,但没有效果。 例如,如果您从同一个线程第二次调用它, CoInitializeEx函数将返回S_FALSE。 如果您需要在代码中区分S_OK和S_FALSE,则应直接测试该值,但仍使用FAILED或SUCCEEDED来处理剩余的情况......

我建议你遵循相同的惯例,例如:

HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);if (SUCCEEDED(hr)){ if (hr != S_FALSE) { // use attribute as needed... } else { // attribute not found... }}else{ // error...}

如果您确实要为不存在的属性返回错误代码,我建议您为该特定条件定义自定义HRESULT ,例如:

#define E_ATTR_NOT_FOUND MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 1)

要么:

#define E_ATTR_NOT_FOUND HRESULT_FROM_WIN32(ERROR_NOT_FOUND)

然后您可以将该错误返回给调用者,例如:

HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);if (SUCCEEDED(hr)){ // use attribute as needed...}else if (hr == E_ATTR_NOT_FOUND){ // attribute not found...}else{ // error...}

COM没有为“未找到”条件定义标准化错误HRESULT代码( HRESULT_FROM_WIN32(ERROR_NOT_FOUND)将是最接近的标准等值)。

S_FALSE is a success value, not an error value. Many of Microsoft's own COM APIs return S_FALSE when the method itself succeeds but the requested data is not available, or the requested operation is not performed. This is mentioned in Microsoft's documentation:

Error Handling in COM

All of the constants with the prefix "E_" are error codes. The constants S_OK and S_FALSE are both success codes. Probably 99% of COM methods return S_OK when they succeed; but do not let this fact mislead you. A method might return other success codes, so always test for errors by using the SUCCEEDED or FAILED macro. ... The success code S_FALSE deserves mention. Some methods use S_FALSE to mean, roughly, a negative condition that is not a failure. It can also indicate a "no-op"—the method succeeded, but had no effect. For example, the CoInitializeEx function returns S_FALSE if you call it a second time from the same thread. If you need to differentiate between S_OK and S_FALSE in your code, you should test the value directly, but still use FAILED or SUCCEEDED to handle the remaining cases...

I suggest you follow the same convention, eg:

HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);if (SUCCEEDED(hr)){ if (hr != S_FALSE) { // use attribute as needed... } else { // attribute not found... }}else{ // error...}

If you really want to return an error code for a non-existing attribute, I suggest you define a custom HRESULT for that specific condition, eg:

#define E_ATTR_NOT_FOUND MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 1)

Or:

#define E_ATTR_NOT_FOUND HRESULT_FROM_WIN32(ERROR_NOT_FOUND)

And then you can return that error to the caller, eg:

HRESULT hr = pImageDetector->GetImageAttr(IMAGE_SPECIAL_ATTRIBUTE, (void*)&attr);if (SUCCEEDED(hr)){ // use attribute as needed...}else if (hr == E_ATTR_NOT_FOUND){ // attribute not found...}else{ // error...}

COM does not define a standardized error HRESULT code for a "not found" condition (HRESULT_FROM_WIN32(ERROR_NOT_FOUND) would be the closest standard equivilent).

发布评论

评论列表(0)

  1. 暂无评论