DbgPrint格式化说明

本文详细介绍了DbgPrint函数的使用方法,包括其最大容量限制、使用举例、说明符及其在不同情况下的应用。同时提供了如何通过Dbgview查看输出内容的指导,并对比了DbgPrint与OutputDebugString的区别。

1. 简单介绍

  Any single call to DbgPrint, DbgPrintEx, KdPrint, or KdPrintEx will only transmit 512 bytes of information. Any output longer than this will be lost. The DbgPrint buffer itself can hold up to 4 KB of data on a free build of Windows, and up to 32 KB of data on a checked build of Windows. 也就是说DbgPrint 中BUFFER容量最大为512字节

DbgPrint (
IN PCH Format,
...
)
{
va_list arglist;
UCHAR Buffer[512];
STRING Output;

//
// Format the output into a buffer and then print it.
//

va_start(arglist, Format);
Output.Length = _vsnprintf(Buffer, sizeof(Buffer), Format, arglist);
Output.Buffer = Buffer;
printf("%s", Buffer);
return 0;
}

 

2. 使用举例

1) 直接打印字符串。

DbgPrint(“Hello World!”);


2) 空结尾的字符串,你可以用普通得C语法表示字符串常量 
char variable_string[] = “Hello World”; 
DbgPrint(“%s”,variable_string);

 

3) 空结尾的宽字符串(WCHAR类型) 
WCHAR    string_w[] = L“Hello World!”; 
DbgPrint(“%ws”,string_w);

或者

DbgPrint(“%S”,string_w);

 

4)Unicode串,由UNICODE_STRING结构描述,包含16位字符。

typedef   struct _UNICODE_STRING{ 
USHORT Length; 
USHORT MaximumLength; 
PWSTR   Buffer; 
}UNICODE_STRING , *PUNICODE_STRING;


UNICODE_STRING    string_unicode = L”Hello World!”; 
DbgPrint(“%wZ\n”,string_unicode.Buffer);   或者DbgPrint(“%wZ\n”,string_unicode);   

 

5) ANSI串,由ANSI_STRING结构描述,包含8位字符。

typedef struct _STRING{ 
USHORT Length; 
USHORT MaximumLength; 
PCHAR   Buffer; 
}STRING, *PANSI_STRING;

STRING bar; 
或者:ANSI_STRING bar; 
RtlInitAnsiString(&bar,”Hello World!”); 
DbgPrint(“%wz\n”,bar.Buffer);

 

DebugPrint格式说明符

符号                                 格式说明符                                        类型
%c, %lc                           ANSI字符                                            char
%C, %wc                         宽字符                                                 wchar_t
%d, %i                            十进制有符号整数                                 int
%D                                  十进制_int64                                     _int64
%L                                  十六进制的LARGE_INTEGER            LARGE_INTEGER
%s, %ls                           NULL终止的ANSI字符串                      char*
%S, %ws                        NULL终止的宽字符串                          wchar_t*
%Z                                 ANSI_STRING字符串
%wZ                              UNICODE_STRING字符串
%u                                 十进制的ULONG                                   ULONG
%x                                小写字符十六进制的ULONG                   ULONG
%X                                大写字符十六进制的ULONG                   ULONG
%p                                指针Pointer 32/64位

 

  根据DDK上说明,Unicode格式(%C, %S, %lc, %ls, %wc, %ws, and %wZ)只能在 IRQL = PASSIVE_LEVEL时才能使用.

 

3. 在哪查看

去微软下载Dbgview.exe

  另外:Dbgview也可以看TRACE输出的内容。但是TRACE只能在MFC下使用;非MFC程序可以使用OutputDebugString,但是此函数只能输出一个字符串,不能做格式化输出,如果需要可以自己封装:

void _trace(TCHAR *format ...)

{

TCHAR buffer[1000];

va_list argptr;

va_start(argptr format);

wvsprintf(buffer format argptr);

va_end(argptr);

OutputDebugString(buffer);}

/* * Copyright(C) 2016,Company 保留所有权利。( All rights reserved. ) * * 文件名称:DbgLog.h * 摘 要: * 当前版本:1.0 * 作 者: * 创建日期:2016年7月19 */ #ifndef _DBGPRINTF_H_ #define _DBGPRINTF_H_ #include <iostream> #include <fstream> #include <tchar.h> #include <string> #include <windows.h> using namespace std; #ifdef _DEBUG #define OFFONPRINTF 1 #else #define OFFONPRINTF 0 #endif #if OFFONPRINTF #define DBG_GET_FILE() __FILE__ #define DBG_GET_FUNCTION() __FUNCTION__ #define DGB_GET_LINE() __LINE__ #else #define DBG_GET_FILE() "" #define DBG_GET_FUNCTION() "" #define DGB_GET_LINE() 0 #endif #define GETFILENAME(x) ((strrchr(x, '\\' )) ? (strrchr(x, '\\') + 1) : (x)) #if OFFONPRINTF #define LOGPRINT(str, ...) \ DbgLog::DbgPrint( \ (char*)GETFILENAME(DBG_GET_FILE()), \ (char*)DBG_GET_FUNCTION(), \ DGB_GET_LINE(), \ (str), ##__VA_ARGS__) #else #define LOGPRINT(str, ...) #endif class DbgLog { public: DbgLog(); ~DbgLog(); /************************************************************************ * 函数名:StartLog * 功 能:开始打印 * 参 数:无 * 返回值:无 ************************************************************************/ static void StartLog(TCHAR *LogName); /************************************************************************ * 函数名:EndLog * 功 能:结束打印 * 参 数:无 * 返回值:无 ************************************************************************/ static void EndLog(); /************************************************************************ * 函数名:DbgPrint * 功 能:输出日志 * 参 数: * @format: * 返回值:无 ************************************************************************/ static void DbgPrint(CHAR* CppName, CHAR *FunName, INT CodeLine, CHAR* format, ...); static void DbgPrint(CHAR* CppName, CHAR *FunName, INT CodeLine, TCHAR* format, ...); protected: private: /************************************************************************ * 函数名:TimeStrLog * 功 能:获取日期 * 参 数: * 返回值:以字符串返回日期格式 ************************************************************************/ static string TimeStrLog(); /************************************************************************ * 函数名:wchar_to_char * 功 能: * 参 数: * 返回值: ************************************************************************/ static int wchar_to_char(char *strGBK, LPCWSTR FileName, int strlen); //ofstream logfile; //wofstream logfileW; //streambuf *outstream; //wstreambuf *outstreamW; static char LogPath[MAX_PATH]; }; #endif 给我增加一个LOGINFO想LOGPRINT一样,但是不一样的是LOGINFO可以release模式输出
最新发布
12-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值