OutputDebugString导致不一致的错误

OutputDebugString causing inconsistent errors

本文关键字:错误 不一致 OutputDebugString      更新时间:2023-10-16

我有一个问题,由于神秘的原因出现和消失。不久前,当我开始这个项目时,我发现了一个相当方便的函数,它允许在VS2010中调试窗口输出。有一段时间效果很好。

现在它不一致地显示错误。这意味着有时代码会编译,有时不会,我不知道下面的代码为什么会导致错误。这看起来几乎是随机的。按compile,error,再按compile而不更改任何内容有时出错有时还好。

这就是错误的样子:

http://clip2net.com/clip/m0/1332710747-clip-29kb.png

http://clip2net.com/clip/m0/1332737362-clip-40kb.png

罪魁祸首是OutputDebugString(buf);注释掉该行时没有出现错误。

我想解决这个问题,我只需要一种将文本输出到调试窗口(输出)的方法,并且正在寻找一个简单、稳定的解决方案。或者,也许有一种方法可以让这个功能发挥作用。我有点被卡住了。

如果你能分享你的做法,我将不胜感激。

代码为:

#pragma once
#ifndef _XDEBUG_H_
#define _XDEBUG_H_
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>

class XDebug
{
public:
    static void __cdecl odprintf(const wchar_t *format, ...){
    wchar_t    buf[4096], *p = buf;
    va_list args;
    int     n;
            va_start(args, format);
            n = _vsnwprintf(p, sizeof buf - 3, format, args); // buf-3 is room for CR/LF/NUL
            va_end(args);
            p += (n < 0) ? sizeof buf - 3 : n;
            while ( p > buf  &&  isspace(p[-1]) )
                    *--p = '';
            *p++ = 'r';
            *p++ = 'n';
            *p   = '';
            OutputDebugString(buf);
    }
};
#endif

OutputDebugString是在Windows.h中定义的。您需要包含该标头才能使用该函数。

看起来你还没有完成:

#include <windows.h>

OutputDebugString函数是Windows API的一部分。