线程监视 OutputDebugString 的调试输出




#include <windows.h>

class CDBWinMonitor 
{
#pragma pack(push, 1)
  typedef struct _tagDBWinBuffer_t
  {
    DWORD   dwProcessId;
    char    data[4096-sizeof(DWORD)];

  }DBWINBUFFER, *LPDBWINBUFFER;
#pragma pack(pop)

private:
  HANDLE m_hDBWinMutex;
  HANDLE m_hDBMonBuffer;
  HANDLE m_hEventBufferReady;
  HANDLE m_hEventDataReady;
  DBWINBUFFER *m_pDBWinBuffer;  
  HANDLE m_hMonitorExitEvent;
  HANDLE m_hWinDebugMonitorThread;
  
public:
  CDBWinMonitor()
  {
    m_hDBWinMutex = NULL;
    m_hDBMonBuffer = NULL;
    m_hEventBufferReady = NULL;
    m_hEventDataReady = NULL;
    m_hWinDebugMonitorThread = NULL;
    m_hMonitorExitEvent = NULL;
    m_pDBWinBuffer = NULL;

    Initialize();
  }
  virtual ~CDBWinMonitor()
  {
    Unintialize();
  }  

#define DbgPrintf printf
  
public:
  virtual void OutputWinDebugString(const DWORD dwID, LPCSTR str) 
  {
    DbgPrintf("%08X:%s", dwID, str);
  };  

protected:
  
  DWORD WinDebugMonitorProcess()
  {    
    HANDLE HEvent[] = {m_hMonitorExitEvent,  m_hEventDataReady};
    DWORD dwWait = WaitForMultipleObjects(2, HEvent, FALSE, INFINITE);
    switch(dwWait)
    {
      case(WAIT_OBJECT_0 + 0): //exit event
      {
        return 0;
      }
      case(WAIT_OBJECT_0 + 1): //data ready
      {
        OutputWinDebugString(m_pDBWinBuffer->dwProcessId, m_pDBWinBuffer->data);
    
        // signal buffer ready
        SetEvent(m_hEventBufferReady);
        break;
      }
      default: //failure
      {
        return 0;
      }
    }
    
    return 1;
  }

  static DWORD CALLBACK WinDebugMonitorThread(LPVOID pData)
  {
    CDBWinMonitor *pThis = (CDBWinMonitor *)pData;
    while(pThis->WinDebugMonitorProcess()) continue;
    return 0;
  }


  void Unintialize()
  {
    if(m_hMonitorExitEvent)
    {
      SetEvent(m_hMonitorExitEvent);
    }

    if (m_hWinDebugMonitorThread)
    {      
      WaitForSingleObject(m_hWinDebugMonitorThread, 2000);
      
      CloseHandle(m_hWinDebugMonitorThread);
      m_hWinDebugMonitorThread = NULL;
    }

    if(m_hMonitorExitEvent)
    {
      CloseHandle(m_hMonitorExitEvent);
      m_hMonitorExitEvent = NULL;
    }

    if(m_pDBWinBuffer)
    {
      UnmapViewOfFile(m_pDBWinBuffer);
      m_pDBWinBuffer;
    }

    if (m_hDBMonBuffer) 
    {
      CloseHandle(m_hDBMonBuffer);
      m_hDBMonBuffer = NULL;
    }

    if (m_hEventDataReady)
    {
      CloseHandle(m_hEventDataReady);
      m_hEventDataReady = NULL;
    }    
    
    if (m_hEventBufferReady)
    {
      CloseHandle(m_hEventBufferReady);
      m_hEventBufferReady = NULL;
    }
    
    if (m_hDBWinMutex)
    {
      CloseHandle(m_hDBWinMutex);
      m_hDBWinMutex = NULL;
    }
  }


  DWORD Initialize()
  {
    DWORD errorCode = 0;
    BOOL bSuccessful = FALSE;

    SetLastError(0);
    do
    {
      // Mutex: DBWin
      // ---------------------------------------------------------    
      if ((m_hDBWinMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, TEXT("DBWinMutex"))) == NULL) 
      {
        errorCode = GetLastError();
        DbgPrintf("OpenMutex failed with %u\n", errorCode);
        break;
      }

      // Event: buffer ready
      // ---------------------------------------------------------
      LPCSTR szDbWinBufferReady = "DBWIN_BUFFER_READY";
      if ((m_hEventBufferReady = OpenEventA(EVENT_ALL_ACCESS, FALSE, szDbWinBufferReady)) == NULL) 
      {
        m_hEventBufferReady = CreateEventA(NULL,FALSE, TRUE, szDbWinBufferReady);
      }
      if (m_hEventBufferReady == NULL) 
      {
        errorCode = GetLastError();
        DbgPrintf("OpenEvent %s failed with %u\n", szDbWinBufferReady, errorCode);
        break;
      }

      // Event: data ready
      // ---------------------------------------------------------
      LPCSTR szDbWinDataReady = "DBWIN_DATA_READY";
      if((m_hEventDataReady = OpenEventA(SYNCHRONIZE, FALSE, szDbWinDataReady)) == NULL)
      {
        m_hEventDataReady = CreateEventA(NULL, FALSE, FALSE, szDbWinDataReady);
      }
      if (m_hEventDataReady == NULL) 
      {
        errorCode = GetLastError();
        DbgPrintf("OpenEvent %s failed with %u\n", szDbWinDataReady, errorCode);
        break;
      }

      // Shared memory
      // ---------------------------------------------------------
      LPCSTR szDbWinBuffer = "DBWIN_BUFFER";
      if((m_hDBMonBuffer = OpenFileMappingA(FILE_MAP_READ, FALSE, szDbWinBuffer)) == NULL)
      {
        m_hDBMonBuffer = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 
          0, sizeof(DBWINBUFFER),  szDbWinBuffer);
      }
      if (m_hDBMonBuffer == NULL)
      {
        errorCode = GetLastError();
        DbgPrintf("OpenFileMapping %s failed with %u\n", szDbWinBuffer, errorCode);
        break;
      }

      if((m_pDBWinBuffer = (DBWINBUFFER *)MapViewOfFile(m_hDBMonBuffer, SECTION_MAP_READ, 
        0, 0, 0)) == NULL)
      {
        errorCode = GetLastError();
        DbgPrintf("MapViewOfFile failed with %u\n", errorCode);
        break;
      }

      // Monitoring thread
      // ---------------------------------------------------------
      if((m_hMonitorExitEvent = CreateEventA(NULL, TRUE, FALSE, NULL)) == NULL)
      {
        errorCode = GetLastError();
        DbgPrintf("CreateEvent for exit event failed with %u\n", errorCode);
        break;
      }

      if((m_hWinDebugMonitorThread = CreateThread(NULL, 0,
        WinDebugMonitorThread,  this,  0,  NULL)) == NULL)
      {
        errorCode = GetLastError();
        DbgPrintf("CreateThread failed with %u\n", errorCode);
        break;
      }

      // set monitor thread's priority to highest
      // ---------------------------------------------------------
      SetPriorityClass( GetCurrentProcess(),  REALTIME_PRIORITY_CLASS);
      SetThreadPriority(m_hWinDebugMonitorThread, THREAD_PRIORITY_TIME_CRITICAL);
    }while(0);

    if(errorCode != ERROR_SUCCESS)
    {
      Unintialize();
    }

    return errorCode;
  }
};

CDBWinMonitor m_DBWinMonitor;

#include <conio.h>

int main(int argc, char* argv[])
{
  OutputDebugStringA("Hello World!\n");

  while(! _kbhit())
  {
    Sleep(1);
    continue;
  }

  return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值