空結尾的字符串,你可以用普通的 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);
分配和釋放字符串緩沖區RtlInitAnsiString 函數初始化 ANSI_STRING 字符串。
RtlInitUnicodeString 函數初始化 UNICODE_STRING 字符串。
RtlAnsiStringToUnicodeString 函數把 ANSI_STRING 轉化成 UNICODE_STRING。
RtlFreeUnicodeString 函數釋放給字符串動態分配的內存。
RtlInitAnsiString 和 RtlInitUnicodeString 初始化時不分配內存。不能使用 RtlFreeUnicodeString 函數. ANSI_STRING 和 UNICODE_STRING 中的 Buffer 指向一個字符串常量,當調用 RtlFreeUnicodeString 時字符串常量占用的地址被釋放。所以產生錯誤。
RtlAnsiStringToUnicodeString 函數被調用時,將為目標變量分配內存。所以在不使用該變量時要用 RtlFreeUnicodeString 函數釋放內存,以免內存泄漏。
例:
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); // 釋放動態分配的內存。
DbgPrint 格式說明符
------------------------------------------------
符號 格式說明符 類型
------------------------------------------------
%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
posted on 2009-07-17 10:46
free2000fly 閱讀(3262)
評論(0) 編輯 收藏 引用