首页 > 编程语言 > C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解
2022
05-15

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()

原因: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-)”。

点击确定,再重新编译,即可。

怎样处理“error C2220: warning trea...”错误

到此这篇关于C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解的文章就介绍到这了,更多相关C语言 OutputDebugString与格式化输出函数OutputDebugPrintf内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧