C语⾔OutputDebugString与格式化输出函数
OutputDebugPrintf案例详解
OutputDebugString属于windows API的,所以只要是包含了window.h这个头⽂件后就可以使⽤了。可以把调试信息输出到编译器的输出窗⼝,还可以⽤DbgView(本机或TCP远程)这样的⼯具查看,这样就可以脱离编译器了。
OutputDebugString 默认只能输⼊⼀个参数,不能像printf那样格式化输出,下⾯改造成类似printf函数的输出⽅式。
#include <windows.h>
#include <stdio.h>
//#include <stdlib.h>
#include <stdarg.h>
#define IS_USE_OUTPUT_DEBUG_PRINT  1
#if  IS_USE_OUTPUT_DEBUG_PRINT
#define  OUTPUT_DEBUG_PRINTF(str)  OutputDebugPrintf(str)
void OutputDebugPrintf(const char * strOutputString, ...)
{
#define PUT_PUT_DEBUG_BUF_LEN  1024
char strBuffer[PUT_PUT_DEBUG_BUF_LEN] = { 0 };
va_list vlArgs;
va_start(vlArgs, strOutputString);
_vsnprintf_s (strBuffer, sizeof(strBuffer) - 1, strOutputString, vlArgs);  //_vsnprintf_s  _vsnprintf
//vsprintf(strBuffer,strOutputString,vlArgs);
va_end(vlArgs);
OutputDebugStringA(strBuffer);  //OutputDebugString    // OutputDebugStringW
}
#else
#define  OUTPUT_DEBUG_PRINTF(str)
#endif
使⽤实例:
OutputDebugPrintf("DEBUG_INFO | %d %s",600019,"hello");
然后在 DbgView 设置⼀个过滤:DEBUG_INFO,抓取固定的输出。
Unicode模式下,OutputDebugString要求⼀个 wchar_t ⽽不是char,⽽sprintf则需要char参数,那我们是不是⼀定要通过字符
转换解决问题呢?
答案就是 OutputDebugStringA()
printf函数是如何实现的原因:Unicode模式,OutputDebugString会变成OutputDebugStringW。如果想⽤ANSI版本的,直接写OutputDebugStringA,或者设置⼯程属性,使⽤MBCS的编码集。
处理“error C2220: warning treated as error - no object file generated”错误"
产⽣原因为:有些Project编译选项中,Treat Warnings As Errors(把警告看作错误来处理)选项开启了。
只要把此选项关闭,就可以正常编译了。
在Solution中,选择⼯程,右键菜单中选择“Properties”。弹出的属性框中,将Configuration选择“All Configurations”,选
择“C/C++/General/”,右侧Treat Warnings As Errors值从原来的“Yes(/WX)”改为“No(/WX-)”。
点击确定,再重新编译,即可。
到此这篇关于C语⾔ OutputDebugString与格式化输出函数OutputDebugPrintf案例详解的⽂章就介绍到这了,更多相关C语⾔OutputDebugString与格式化输出函数OutputDebugPrintf内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!