【转】内核打印字符串 DbgPrint

来源:互联网 发布:淘宝手提包拍摄 编辑:程序博客网 时间:2024/06/05 19:41

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

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

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(“%ws/n”,string_unicode.Buffer);     

 

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(“%s/n”,bar.Buffer);5.                      分配和释放字符串缓冲区

RtlInitAnsiString函数初始化ANSI_STRING字符串。
RtlInitUnicodeString函数初始化UNICODE_STRING字符串。
RtlAnsiStringToUnicodeString函数把ANSI_STRING转化成UNICODE_STRING。
RtlFreeUnicodeString函数释放给字符串动态分配的内存。

RtlInitAnsiString和RtlInitUnicodeString初始化时不分配内存。不能使用RtlFreeUnicodeString函数。ANSI_STRING和UNICODE_STRING中的Buffer指向一个字符串常量,当调用RtlFreeUnicodeString时字符串常量占用的地址被释放。所以产生错误。
RtlAnsiStringToUnicodeString该函数被调用时,将为目标变量分配内存。所以在不使用该变量时要用该函数释放内存,以免内存泄漏。

 

例:
UNICODE_STRING string_unicode;
ANSI_STRING   string_ansi;
RtlInitUnicodeString(&string_unicode,L”Hello World!”);//不用释放内存
RtlInitAnsiString(&string_ansi,”Hello World!”);
RtlAnsiStringToUnicodeString(&string_unicode,&string_ansi,TRUE);//需要释放内存。
RtlFreeUnicodeString(&string_unicode);//释放动态分配的内存。

DebugPrint格式说明符
------------------------------------------------
符号   格式说明符     类型
------------------------------------------------
%c   ANSI字符     char
%C   宽字符      wchar_t
%d,%i   十进制有符号整数    int
%D   十进制_int64     _int64
%I   IRP主功能代码和次功能代码   PIRP
%L   十六进制的LARGE_INTEGER    LARGE_INTEGER
%s   NULL终止的ANSI字符串    char*
%S   NULL终止的宽字符串    wchar_t*
%T   UNICODE_STRING     PUNICODE_STRING
%u   十进制的ULONG     ULONG
%x   十六进制的ULONG     ULONG

 

 

原创粉丝点击