Windows Driver Model

__try __except在使用上的限制


如果使用者使用TryException作為存取無效記憶體的例外處理時,應該要特別注意,這樣的機制並無法適用於高記憶體位址的驗證。

範例 1:

#include <wdm.h>

NTSTATUS DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
{
  __try{
    unsigned long *test = 0;
    *test = 0xffffffffffffffff;
  }
  __except(EXCEPTION_EXECUTE_HANDLER){
    DbgPrint("Exception is 0x%x\n", GetExceptionCode());
  }
 
  return STATUS_DEVICE_NOT_READY;
}
Exception is 0xc0000005

範例 2:

#include <wdm.h>

NTSTATUS DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
{
  __try{
    unsigned long *test = 0xeeeeeeeeeeeeeeee;
    *test = 0xffffffffffffffff;
  }
  __except(EXCEPTION_EXECUTE_HANDLER){
    DbgPrint("Exception is 0x%x\n", GetExceptionCode());
  }
 
  return STATUS_DEVICE_NOT_READY;
}
PAGE_FAULT_IN_NONPAGED_AREA (50)
Invalid system memory was referenced.  This cannot be protected by try-except,
it must be protected by a Probe.  Typically the address is just plain bad or it
is pointing at freed memory.

Arguments:
Arg1: eeeeeeee, memory referenced.
Arg2: 00000001, value 0 = read operation, 1 = write operation.


返回上一頁