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


    有没有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 ", "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 = '';
    18         *p++ = '
    ';
    19         *p++ = '
    ';*p = '';
    20     }
    21     OutputDebugString(buf);
    22 } 

    使用方法:

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

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

  • 相关阅读:
    js基础四
    序列化和反序列化
    数组
    枚举
    Class对象和反射
    字符串String
    对象的克隆
    异常处理机制
    多继承和代码块
    接口和抽象类
  • 原文地址:https://www.cnblogs.com/galoishelley/p/3510670.html
Copyright © 2020-2023  润新知