win32使用OutputDebugString输出调试信息的方法

在win32程序中可以使用函数OutputDebugString输出调试信息。输出的结果可以在vs的集成环境中看到,也可以使用工具DbgView.exe捕捉结果。函数的原形如下:

OutputDebugString

The OutputDebugString function sends a string to the debugger for display.

void OutputDebugString( LPCTSTR lpOutputString);
Parameters
lpOutputString
[in] Pointer to the null-terminated string to be displayed.
Return Values
This function does not return a value

因为OutputDebugString的参数是字符串,而我们在实际使用过程中希望能像printf一样支持变参。可以用下面的方法实现这个效果:

inline bool MyDbgStr(LPCSTR lpszFormat, ...)
{
    va_list   args;
    int       nBuf;
    TCHAR     szBuffer[512];

    va_start(args, lpszFormat);

    nBuf = _vsnprintf(szBuffer, sizeof(szBuffer)*sizeof(TCHAR), lpszFormat, args);
    Assert(nBuf > 0);
    OutputDebugString(szBuffer);

    va_end(args);
}

原文地址:https://www.cnblogs.com/lidabo/p/2837420.html