为什么DebugActiveProcessStop会崩溃我的调试应用程序?

时间:2012-03-22 23:03:48

标签: debugging

我有一个调试程序,我已写入该程序以附加到进程并创建故障转储文件。那部分工作正常。

我遇到的问题是,当调试程序终止时,调试程序也会终止。

我做了一些谷歌搜索并找到了DebugActiveProcessStop()API调用。这没有出现在我的旧版MSDN文档中,因为它仅在Windows XP中引入,因此我尝试在运行时从Kernel32.dll加载它动态。

现在我的问题是我的调试程序在调用_DebugActiveProcessStop()后立即崩溃。有人可以告诉我我做错了吗?

typedef BOOL (*DEBUGACTIVEPROCESSSTOP)(DWORD);

DEBUGACTIVEPROCESSSTOP _DebugActiveProcessStop;

HMODULE hK32 = LoadLibrary( "kernel32.dll" );

if( hK32 )
  _DebugActiveProcessStop = (DEBUGACTIVEPROCESSSTOP) GetProcAddress( hK32,"DebugActiveProcessStop" );
else
{
  printf( "Can't load Kernel32.dll\n" );
  return;
}

if( ! _DebugActiveProcessStop )
{
  printf( "Can't find DebugActiveProcessStop\n" );
  return;
}

...

void DebugLoop( void )
{
  DEBUG_EVENT de;

  while( 1 )
  {
    WaitForDebugEvent( &de, INFINITE ); 

    switch( de.dwDebugEventCode )
    {
      case CREATE_PROCESS_DEBUG_EVENT:
        hProcess = de.u.CreateProcessInfo.hProcess;
        break;

      case EXCEPTION_DEBUG_EVENT: 

        // PDS: I want a crash dump immediately!
        dwProcessId = de.dwProcessId;
        dwThreadId  = de.dwThreadId;

        WriteCrashDump( &de.u.Exception );
        return;

      case CREATE_THREAD_DEBUG_EVENT:
      case OUTPUT_DEBUG_STRING_EVENT:
      case EXIT_THREAD_DEBUG_EVENT:
      case EXIT_PROCESS_DEBUG_EVENT :
      case LOAD_DLL_DEBUG_EVENT:
      case UNLOAD_DLL_DEBUG_EVENT:
      case RIP_EVENT:
      default:
        break;
    }

    ContinueDebugEvent( de.dwProcessId, de.dwThreadId, DBG_CONTINUE );
  }
}

...
void main( void )
{
...
  BOOL bo = DebugActiveProcess( dwProcessId );

  if( bo == 0 )
    printf( "DebugActiveProcess failed, GetLastError: %u \n",GetLastError() );

  hProcess = OpenProcess( PROCESS_ALL_ACCESS, TRUE, dwProcessId );

  if( hProcess == NULL )
    printf( "OpenProcess failed, GetLastError: %u \n",GetLastError() );

  DebugLoop();

  _DebugActiveProcessStop( dwProcessId );

  CloseHandle( hProcess );
}

1 个答案:

答案 0 :(得分:0)

崩溃的原因是因为我错过了函数指针定义中的WINAPI关键字。

这有效:

typedef BOOL(WINAPI * DEBUGSETPROCESSKILLONEXIT)(BOOL);