驱动下的异常处理

返回状态值

  检查内存的可用性

  异常处理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

 

RReserved)保留位

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蚂蚁_CrkRes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值