C語言 OutputDebugString與格式化輸出函數(shù)OutputDebugPrintf案例詳解
OutputDebugString屬于windows API的,所以只要是包含了window.h這個頭文件后就可以使用了??梢园颜{(diào)試信息輸出到編譯器的輸出窗口,還可以用DbgView(本機或TCP遠程)這樣的工具查看,這樣就可以脫離編譯器了。

OutputDebugString 默認只能輸入一個參數(shù),不能像printf那樣格式化輸出,下面改造成類似printf函數(shù)的輸出方式。
#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 設(shè)置一個過濾:DEBUG_INFO,抓取固定的輸出。
Unicode模式下,OutputDebugString要求一個 wchar_t 而不是char,而sprintf則需要char參數(shù),那我們是不是一定要通過字符轉(zhuǎn)換解決問題呢?
答案就是 OutputDebugStringA()
原因:Unicode模式,OutputDebugString會變成OutputDebugStringW。如果想用ANSI版本的,直接寫OutputDebugStringA,或者設(shè)置工程屬性,使用MBCS的編碼集。
處理“error C2220: warning treated as error - no object file generated”錯誤"
產(chǎn)生原因為:有些Project編譯選項中,Treat Warnings As Errors(把警告看作錯誤來處理)選項開啟了。
只要把此選項關(guān)閉,就可以正常編譯了。
在Solution中,選擇工程,右鍵菜單中選擇“Properties”。彈出的屬性框中,將Configuration選擇“All Configurations”,選擇“C/C++/General/”,右側(cè)Treat Warnings As Errors值從原來的“Yes(/WX)”改為“No(/WX-)”。
點擊確定,再重新編譯,即可。

到此這篇關(guān)于C語言 OutputDebugString與格式化輸出函數(shù)OutputDebugPrintf案例詳解的文章就介紹到這了,更多相關(guān)C語言 OutputDebugString與格式化輸出函數(shù)OutputDebugPrintf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

