软件编程
位置:首页>> 软件编程>> C语言>> C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解

C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解

作者:EmbededCoder  发布时间:2023-11-02 16:21:47 

标签:C语言,OutputDebugString,OutputDebugPrintf

OutputDebugString属于windows API的,所以只要是包含了window.h这个头文件后就可以使用了。可以把调试信息输出到编译器的输出窗口,还可以用DbgView(本机或TCP远程)这样的工具查看,这样就可以脱离编译器了。  

C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解

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-)”。

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

C语言 OutputDebugString与格式化输出函数OutputDebugPrintf案例详解

来源:https://blog.csdn.net/u012308586/article/details/92992951

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com