记录一个vs下的调试方法

通过 win API OutputDebugString() 函数,将格式化的字符串输出至调试窗口,该字符串不会出现再运行程序的过程中

OutputDebugString() 用在ANSI下,Unicode下需要 LPCWSTR ,char * 需要使用OutputDebugStringA()

#include <iostream>
#include <stdio.h>
#include <Windows.h>

typedef unsigned int U32;

int VDebugPrintF(const char* format, va_list arglist) {
    const U32 MAX_CHARS = 1024;
    static char s_buffer[MAX_CHARS];

    int charsWritten = vsnprintf(s_buffer, MAX_CHARS, format, arglist); // 生成格式化字符串

    OutputDebugStringA(s_buffer); // vs 调试字符串输出 Unicode模式
    return charsWritten;
}
// va_list 可变参数宏

int DebugPrintF(const char* format, ...) {
    va_list arglist;
    va_start(arglist, format);
    int charsWritten = VDebugPrintF(format, arglist);
    va_end(arglist);
    return charsWritten;
}

int main()
{
    DebugPrintF("Test Debug %d\n", 1);
    return 0;
}
posted @ 2021-03-11 21:37  springfield_psk  阅读(66)  评论(0编辑  收藏  举报