OutputDebugStringAPI函数包括MultiChar和Unicode两种版本:OutputDebugStringA和OutputDebugStringW,原型:如下:
void
__stdcall
OutputDebugStringA(
const char*lpOutputString
);
void
__stdcall
OutputDebugStringW(
const wchar_tlpOutputString
);
使用起来非常简单,只有一个参数而--且没有返回值.
DebugWindow的实现:
由2个事件,1个系统缓冲区的映射指针来实现.
1个事件用来得到对系统缓冲区的访问权,另一个用来通过对系统缓冲区操作完毕.
缓冲区的声明为:
#pragma pack(push)
#pragma pack(1)
typedef struct __DEBUGBUFFER
{
DWORD sz;
char szString[4096 - sizeof(DWORD)];//这里保存了OutputDebugString输出的字符串
} DEBUGBUFFER, *PDEBUGBUFFER;
#pragma pack(pop)
请求访问缓冲区的事件名为:"DBWIN_BUFFER_READY"
通知缓冲区有新数据的事件名为:"DBWIN_DATA_READY"
系统缓冲区对象名为:"DBWIN_BUFFER"
实例代码:
// DebugOutCapture.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#pragma pack(push)
#pragma pack(1)
typedef struct __DEBUGBUFFER
{
DWORD sz;
char szString[4096 - sizeof(DWORD)];
} DEBUGBUFFER, *PDEBUGBUFFER;
#pragma pack(pop)
;
#define MAX_DEBUG_BUF_LEN (4096)
#define _Try __try
#define _Finally __finally
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE m_hReadyEvent;
DWORD m_dwResult;
HANDLE hMapping = NULL;
HANDLE hAckEvent = NULL;
PDEBUGBUFFER pdbBuffer = NULL;
//TCHAR tzBuffer[MAX_DEBUG_BUF_LEN];
_Try
{
#define _LeaveIf(expr) if(expr == TRUE) /
returnFALSE;
// 设置初始结果
m_dwResult =ERROR_INVALID_HANDLE;
// 打开事件句柄
hAckEvent = CreateEvent(NULL,FALSE, FALSE, TEXT("DBWIN_BUFFER_READY"));
_LeaveIf(hAckEvent ==NULL);
m_hReadyEvent =CreateEvent(NULL, FALSE, FALSE, TEXT("DBWIN_DATA_READY"));
_LeaveIf(m_hReadyEvent ==NULL);
// 创建文件映射
hMapping =CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,MAX_DEBUG_BUF_LEN, TEXT("DBWIN_BUFFER"));
_LeaveIf(hMapping ==NULL);
// 映射调试缓冲区
pdbBuffer = (PDEBUGBUFFER)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
_LeaveIf(pdbBuffer ==NULL);
// 循环
for (m_dwResult =ERROR_SIGNAL_PENDING; (m_dwResult == ERROR_SIGNAL_PENDING); )
{
//等待缓冲区数据
SetEvent(hAckEvent);
if(WaitForSingleObject(m_hReadyEvent, INFINITE) ==WAIT_OBJECT_0)
{
//如果是继续等待,否则表示主线程发出了停止信号,退出当前线程
if(m_dwResult == ERROR_SIGNAL_PENDING)
{
//添加新项
printf(pdbBuffer->szString);
}
}
else
{
//等待失败
m_dwResult= WAIT_ABANDONED;
}
}
}
_Finally
{
#define _SafeCloseHandle(handle) if(handle != NULL) /
CloseHandle(handle);
// 释放
if (pdbBuffer)
{
UnmapViewOfFile(pdbBuffer);
}
_SafeCloseHandle(hMapping);
_SafeCloseHandle(m_hReadyEvent);
_SafeCloseHandle(hAckEvent);
// 返回结果
return m_dwResult;
}
}
一个非常典型的应用就是在VisualStudio之中IDE的输出子窗体,它其实就是一个Debug信息接收窗体,只需在源码中为OutputDebugString函数输入需要输出的调试信息就可以了,例子:
CString strDebugOutput;
strDebugOutput.Format("This is debug infor/n");
OutputDebugString(strDebugOutput.GetBuffer());
运行就可以看到在输出窗体中的Debug信息输出.
实例和测试工程请从这里下载:
http://download.csdn.net/source/940777