win32程序调试OutputDebugString 类似printf格式化输出

有没有win32编程因为打印变量调试程序而头疼呢.方法二的函数完全类似printf.非常完美.
方法一:

不带参数输出如printf("hello world");
OutputDebugString("debug");
 case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDS_BTN1:
OutputDebugString("1");
break;
case IDS_CLEAR:
SetWindowText(GetDlgItem(hWnd,IDS_EDIT),(LPCTSTR)"");
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break; 

使用工具debug view查看:

win32程序调试OutputDebugString 类似printf格式化输出

方法二:

带参数输出入如 printf("hello %s welcome this %d\n", "world", "501");

 #include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <windows.h> void __cdecl odprintf(const char *format, ...)
{
char buf[], *p = buf;
va_list args; va_start(args, format);
p += _vsnprintf(p, sizeof buf - , format, args);
va_end(args); while ( p > buf && isspace(p[-]) )
{
*--p = '\0';
*p++ = '\r';
*p++ = '\n';*p = '\0';
}
OutputDebugString(buf);
} 

使用方法:

odprintf("Cannot open file %s [err=%ld]", "test.c", 110201);

编译运行之后使用方法一查看

上一篇:硬刚 lodash 源码之路,compact & concat


下一篇:学习C# XmlSerializer 序列化反序列化XML