2012-03-13 33 views
0

任何有關開始使用示例代碼的幫助信息,請參見C++/CLI Win32 debugger library for x86以監視進程異常。通過調試API監視進程異常

一些代碼,我做的是:

 
using System; 
using DebugLibrary; 

namespace DebugTeste01 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      DebugUtil.DebugActiveProcess(4932); 
      DebugEvent de = new DebugEvent(); 
      ThreadContext tc = new ThreadContext(); 
      LDTEntry ldte = new LDTEntry(); 

      do 
      { 
       debug_evt = DebugUtil.WaitForDebugEvent(0xffffffff); 

       de = (DebugEvent)debug_evt; 
       Process proc = Process.GetProcessById(de.processId); 

       object meminfo = DebugUtil.GetMemoryInfo(proc.Handle); 
       //... 
       object modinf = DebugUtil.GetModuleInfo(proc.Handle); 
       //... 

       switch (debug_evt.GetType().ToString()) 
       { 
        case "DebugLibrary.DebugEvent_CreateProcess": 
         { 
          DebugEvent_CreateProcess decp = (DebugEvent_CreateProcess)debug_evt; 
          //some action, logging, etc. 
         } 
         break; 
        case "DebugLibrary.DebugEvent_LoadDll": 
         { 
          DebugEvent_LoadDll dect = (DebugEvent_LoadDll)debug_evt; 
          //some action, logging, etc. 
         } 
         break; 
        case "DebugLibrary.DebugEvent_CreateThread": 
         { 
          DebugEvent_CreateThread dect = (DebugEvent_CreateThread)debug_evt; 
          //some action, logging, etc. 
         } 
         break; 
        case "DebugLibrary.DebugEvent_ExitThread": 
         { 
          DebugEvent_ExitThread dect = (DebugEvent_ExitThread)debug_evt; 
          //some action, logging, etc. 
         } 
         break; 
        case "DebugLibrary.DebugEvent_Exception": 
         { 
          DebugEvent_Exception dect = (DebugEvent_Exception)debug_evt; 

          ExceptionRecord exbp = dect.exceptionRecord; 

          switch (exbp.GetType().ToString()) 
          { 
           case "Breakpoint": 
            { 
             //some action, logging, etc. 
             exbp = null; 
            } 
            break; 
           case "AccessViolation": 
            { 
             //some action, logging, etc. 
             exbp = null; 
            } 
            break; 
           //more case 
          } 
         } 
         break; 
        default: 
         { 
          //some action, logging, etc. 
          debug_evt = null; 
         } 
         break; 
       } 

       try 
       { 
        DebugUtil.ContinueDebugEvent(de.processId, de.threadId, false); 
       } 
       catch 
       { 
        break; 
       } 
      } 
      while (true); 
     } 
    } 
} 

[編輯] 2012年3月14日
好文章:Using the Windows Debugging API
[編輯] 2012年3月14日
改進執行工作。
現在它有一個最終應用程序的初始骨架結構。

回答

1

對我來說,你是在調試器編寫了解資源之後,在這種情況下,你最好的開始的地方是MSDN,這給出了應該如何處理的基礎知識。從那裏它真正地取決於您的應用程序和環境如何處理異常。

作爲對實際代碼的評論,請避免硬編碼爲PID,而應使用進程名稱。

+0

代碼的目的是展示如何使用API​​,但首先必須瞭解它的工作原理。 – lsalamon 2012-03-13 18:36:16