11/12/2007

How to Output Debug Message in Visual C++ Debug Mode's Output Area

DbgMessage.h
#ifndef _DBGMESSAGE_H_
#define _DBGMESSAGE_H_

void __cdecl DbgPrintf(LPCTSTR format, ...);

#endif /*_DBGMESSAGE_H_*/

#ifndef DBG_MAX_BUFFER_SIZE

#define DBG_MAX_BUFFER_SIZE 256

#endif /*_DBG_MAX_BUFFER_SIZE_*/

DbgMessage.cpp
#include "stdafx.h"

void __cdecl DbgPrintf(LPCTSTR format, ...)
{
static TCHAR DbgMsg[DBG_MAX_BUFFER_SIZE];
size_t len;
TCHAR *p = DbgMsg;
va_list args;

va_start(args, format);
StringCchVPrintf(p, sizeof(DbgMsg) - 1, format, args);
StringCchLength(DbgMsg, sizeof(DbgMsg) - 1, &len);
p += len;
va_end(args);

*p++ = '\r';
*p++ = '\n';
*p = '\0';

OutputDebugString(DbgMsg);
}



Usage:
int a = 101;
DbgPrintf(TEXT("hello %d"), a);

沒有留言: