两个问题:
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)
不应该是“状态== 0”吗?
__ccess是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(return >= 0)
是SAL注释,它为PreFast工具提供了关于宏的预期语义的线索。这用于进行静态分析并识别潜在的错误。
NT_SUCCESS
宏测试>= 0
,因为除了STATUS_SUCCESS
之外还有成功代码。一些成功代码包括有关操作结果的额外信息,尽管目前我只能想到S_FALSE
,它通知调用者操作成功,但结果是错误的。通常,成功代码等于或大于零,故障代码小于零。
[严格来说,S_FALSE
是HRESULT
,而不是NT_STATUS
,虽然这两种类型具有相同的大小和相似的约定。]
__success很好地描述了dead-link。
对2的回答是否,所有正面代码都是非故障。它们可能意味着除了OK之外的其他东西。