返回状态值
检查内存的可用性
异常处理try-except
异常处理try-finally
断言
NTSTATUS
typedef LONG NTSTATUS;
NT_SUCCESS
#define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) // ntsubauth
//
// Values are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
//
// Define the facility codes
R(Reserved)保留位
C (Customer) 客户位
Sev(Severity) 重要位 共2个二进制位 00表示成功 01表示信息 10表示警告 11表示错误
检测内存可用性
ProbeForRead
VOID ProbeForRead(
__in PVOID Address,
__in SIZE_T Length,
__in ULONG Alignment
);
ProbeForWrite
VOID ProbeForWrite(
__in PVOID Address,
__in SIZE_T Length,
__in ULONG Alignment
);
void Memaccess_Test()
{
KdPrint(("测试内存可用否\n"));
int i, *pi=NULL;
__try
{
i=*pi;
}
__except(1)
{
KdPrint(("测试内存 不可用\n"));
return;
}
}
void ProbeForRead_Test()
{
KdPrint(("测试内存可用否\n"));
int *pi=NULL;
__try
{ ProbeForRead(pi,4,1);
//i=*pi;
}
__except(1)
{
KdPrint(("ProbeForRead 测试内存 不可用\n"));
return;
}
}
结构化异常处理try except
__try
{
//这里如果出错 发出异常
}
__except(filter_Value)
{
}
filter_Value是以下三种值之一
EXCEPTION_CONTINUE_SEARCH 0 转向上一层异常处理
EXCEPTION_CONTINUE_EXECUTION -1 重复执行错误指令
EXCEPTION_EXECUTE_HANDLER 1 忽略该错误 转到 __except 块处理
结构化异常处理try finally
__try
{
}
__finally
{
}
断言
ASSERT
#if DBG
#define ASSERT( exp ) \
((!(exp)) ? \
(RtlAssert( #exp, __FILE__, __LINE__, NULL ),FALSE) : \
TRUE)
#else
#define ASSERT( exp ) ((void) 0)
RtlAssert
侧效(宏使用问题)
#define add(a,b) a=a+b; b=a+b;
if (??) add(a,b)