空結(jié)尾的字符串,你可以用普通的 C 語法表示字符串常量
1) DbgPrint(“Hello World!”); //直接打印字符串。
2) char variable_string[] = “Hello World”;
DbgPrint(“%s”,variable_string);
空結(jié)尾的寬字符串(WCHAR類型)
WCHAR string_w[] = L“Hello World!”;
DbgPrint(“%ws”,string_w);
Unicode 串,由 UNICODE_STRING 結(jié)構(gòu)描述, 包含 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結(jié)構(gòu)描述,包含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);
分配和釋放字符串緩沖區(qū)RtlInitAnsiString 函數(shù)初始化 ANSI_STRING 字符串。
RtlInitUnicodeString 函數(shù)初始化 UNICODE_STRING 字符串。
RtlAnsiStringToUnicodeString 函數(shù)把 ANSI_STRING 轉(zhuǎn)化成 UNICODE_STRING。
RtlFreeUnicodeString 函數(shù)釋放給字符串動態(tài)分配的內(nèi)存。
RtlInitAnsiString 和 RtlInitUnicodeString 初始化時不分配內(nèi)存。不能使用 RtlFreeUnicodeString 函數(shù). ANSI_STRING 和 UNICODE_STRING 中的 Buffer 指向一個字符串常量,當(dāng)調(diào)用 RtlFreeUnicodeString 時字符串常量占用的地址被釋放。所以產(chǎn)生錯誤。
RtlAnsiStringToUnicodeString 函數(shù)被調(diào)用時,將為目標(biāo)變量分配內(nèi)存。所以在不使用該變量時要用 RtlFreeUnicodeString 函數(shù)釋放內(nèi)存,以免內(nèi)存泄漏。
例:
UNICODE_STRING string_unicode;
ANSI_STRING string_ansi;
RtlInitUnicodeString(&string_unicode,L”Hello World!”);//不用釋放內(nèi)存
RtlInitAnsiString(&string_ansi,”Hello World!”);
RtlAnsiStringToUnicodeString(&string_unicode,&string_ansi,TRUE);//需要釋放內(nèi)存。
RtlFreeUnicodeString(&string_unicode); // 釋放動態(tài)分配的內(nèi)存。
DbgPrint 格式說明符
------------------------------------------------
符號 格式說明符 類型
------------------------------------------------
%c ANSI字符 char
%C 寬字符 wchar_t
%d,%i 十進(jìn)制有符號整數(shù) int
%D 十進(jìn)制_int64 _int64
%I IRP主功能代碼和次功能代碼 PIRP
%L 十六進(jìn)制的LARGE_INTEGER LARGE_INTEGER
%s NULL終止的ANSI字符串 char*
%S NULL終止的寬字符串 wchar_t*
%T UNICODE_STRING PUNICODE_STRING
%u 十進(jìn)制的ULONG ULONG
%x 十六進(jìn)制的ULONG ULONG
posted on 2009-07-17 10:46
free2000fly 閱讀(3262)
評論(0) 編輯 收藏 引用