如何理解“NTSTATUS”、“NT_SUCCESS” Windows ddk 中的 typedef ?

发布于 2024-09-12 06:24:04 字数 518 浏览 3 评论 0原文

两个问题:

1.

在“ntdef.h”中,NTSTATUS定义如下:

typedef __success(return >= 0) LONG NTSTATUS;

“__success(return >= 0)”到底是什么?

2、

在“ntstatus.h”中,STATUS_SUCCESS被定义为0。

#define STATUS_SUCCESS   ((NTSTATUS)0x00000000L) // ntsubauth

但是“ntdef.h”中的NT_SUCCESS宏是:

#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)

不是应该是“Status == 0”?

Two questions:

1.

In "ntdef.h" the NTSTATUS is defined as follow:

typedef __success(return >= 0) LONG NTSTATUS;

what the hell is the "__success(return >= 0)"?

2.

In "ntstatus.h", STATUS_SUCCESS is defined to 0.

#define STATUS_SUCCESS   ((NTSTATUS)0x00000000L) // ntsubauth

But the NT_SUCCESS macro in "ntdef.h" is:

#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)

Shouldn't it be "Status == 0" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2024-09-19 06:24:04

__success 是 SpecStrings_strict.h 中定义的“高级注释”,其定义如下。

*  __success(expr) T f() :  indicates whether function f succeeded or
*  not. If  is true at exit, all the function's guarantees (as given
*  by other annotations) must hold. If  is false at exit, the caller
*  should not expect any of the function's guarantees to hold. If not used,
*  the function must always satisfy its guarantees. Added automatically to
*  functions that indicate success in standard ways, such as by returning an
*  HRESULT.

NT_SUCCESS 没有对 STATUS_SUCCESS (0) 进行严格测试的原因可能是其他代码(如 STATUS_PENDING)实际上并不是失败。

__success is an "Advanced Annotation" defined in SpecStrings_strict.h, which defines it as follows.

*  __success(expr) T f() :  indicates whether function f succeeded or
*  not. If  is true at exit, all the function's guarantees (as given
*  by other annotations) must hold. If  is false at exit, the caller
*  should not expect any of the function's guarantees to hold. If not used,
*  the function must always satisfy its guarantees. Added automatically to
*  functions that indicate success in standard ways, such as by returning an
*  HRESULT.

The reason that NT_SUCCESS doesn't do a strict test against STATUS_SUCCESS (0) is probably that other codes like STATUS_PENDING aren't actually failures.

挽梦忆笙歌 2024-09-19 06:24:04

片段 __success(return >= 0) 是一个 SAL 注释,它为 PreFast 工具提供了有关宏的预期语义的线索。这用于进行静态分析并识别潜在的错误。

NT_SUCCESS 宏测试 >= 0,因为除了 STATUS_SUCCESS 之外还有其他成功代码。一些成功代码包含有关操作结果的额外信息,尽管目前我只能想到S_FALSE,它通知调用者操作成功,但结果是错误的。通常,成功代码等于或大于零,失败代码小于零。

[严格来说,S_FALSEHRESULT,而不是 NT_STATUS,尽管这两种类型具有相同的大小和相似的约定。]

The fragment __success(return >= 0) is a SAL annotation, which gives a clue to the PreFast tool about what the intended semantics of the macro are. This is used to do static analysis and identify potential bugs.

The NT_SUCCESS macro tests for >= 0 because there are success codes other than STATUS_SUCCESS. Some success codes include extra information about the outcome of the operation, although at the moment I can only think of S_FALSE, which notifies the caller that the operation succeeded, but the result was false. As a rule, success codes are equal to or greater than zero, and failure codes are less than zero.

[Strictly speaking, S_FALSE is an HRESULT, not an NT_STATUS, though the two types have the same size and similar conventions.]

无所谓啦 2024-09-19 06:24:04

Michael Fourre 的文章 Annotating for __success() 很好地描述了 __success (存档链接)。

2 的答案是“否”,所有正代码都没有失败。但它们可能意味着“OK”以外的其他含义。

__success is described nicely in Annotating for __success() article by Michael Fourre (archived link).

Answer to 2 is No, all positive codes are non-failures. They may mean something other than OK though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文