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

本文介绍Win32编程中如何通过OutputDebugString和自定义printf函数进行变量调试,包括两种方法的实现与使用方法,以及使用debugview工具查看调试信息的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

不带参数输出如printf("hello world");
OutputDebugString("debug");
 1 case WM_COMMAND:
 2 wmId    = LOWORD(wParam); 
 3 wmEvent = HIWORD(wParam); 
 4 // Parse the menu selections:
 5 switch (wmId)
 6 {
 7     case IDS_BTN1:
 8         OutputDebugString("1");
 9         break;
10     case IDS_CLEAR:
11         SetWindowText(GetDlgItem(hWnd,IDS_EDIT),(LPCTSTR)"");
12         break;
13     case IDM_ABOUT:
14         DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
15         break;
16     case IDM_EXIT:
17         DestroyWindow(hWnd);
18         break;
19     default:
20         return DefWindowProc(hWnd, message, wParam, lParam);
21 }
22 break; 

使用工具debug view查看:

 

方法二:

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

 1 #include <stdio.h>
 2 #include <stdarg.h>
 3 #include <stdlib.h>
 4 #include <windows.h>
 5 
 6 void __cdecl odprintf(const char *format, ...)
 7 {
 8     char buf[4096], *p = buf;
 9     va_list args;
10     
11     va_start(args, format);
12     p += _vsnprintf(p, sizeof buf - 1, format, args);
13     va_end(args);
14     
15     while ( p > buf && isspace(p[-1]) )
16     {
17         *--p = '\0';
18         *p++ = '\r';
19         *p++ = '\n';*p = '\0';
20     }
21     OutputDebugString(buf);
22 } 

使用方法:

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

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

 

 

 

转载于:https://www.cnblogs.com/galoishelley/p/3510670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值