如何理解“NTSTATUS”、“NT_SUCCESS” Windows ddk 中的 typedef ?
两个问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
__success 是 SpecStrings_strict.h 中定义的“高级注释”,其定义如下。
NT_SUCCESS
没有对STATUS_SUCCESS (0)
进行严格测试的原因可能是其他代码(如STATUS_PENDING
)实际上并不是失败。__success is an "Advanced Annotation" defined in SpecStrings_strict.h, which defines it as follows.
The reason that
NT_SUCCESS
doesn't do a strict test againstSTATUS_SUCCESS (0)
is probably that other codes likeSTATUS_PENDING
aren't actually failures.片段
__success(return >= 0)
是一个 SAL 注释,它为 PreFast 工具提供了有关宏的预期语义的线索。这用于进行静态分析并识别潜在的错误。NT_SUCCESS
宏测试>= 0
,因为除了STATUS_SUCCESS
之外还有其他成功代码。一些成功代码包含有关操作结果的额外信息,尽管目前我只能想到S_FALSE
,它通知调用者操作成功,但结果是错误的。通常,成功代码等于或大于零,失败代码小于零。[严格来说,
S_FALSE
是HRESULT
,而不是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 thanSTATUS_SUCCESS
. Some success codes include extra information about the outcome of the operation, although at the moment I can only think ofS_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 anHRESULT
, not anNT_STATUS
, though the two types have the same size and similar conventions.]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.