char型,记录ansi字符集。每个字符一个字节。以0标志结束。在KdPrint中用%s输出。
宽字符型,wchar_t,描述unicode字符集的字符串,每个字符两个字节,以0标志结束。通过L来体现。在KdPrint中用%S输出。
CHAR *string = "Hello";
WCHAR *string2 = L"hello";
KdPrint("%s\n", string);
KdPrint("%S\n", string2);
DDK不鼓励用1)中所示的C语言字符串,因为C的字符串处理函数易导致缓冲区溢出等错误(如忘记对长度进行检查)。而鼓励用DDK自己定义的字符串。
typedef struct _STRING {
USHORT Length; //字符的长度
USHORT MaximumLength;//整个字符缓冲区的最大长度
PCHAR Buffer; //缓冲区的指针
} ANSI_STRING *PANSI_STRING;
注意STRING不是以0结束的。
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING *PUNICODE_STRING;
用%Z输出 ANSI_STRING字符串,用%wZ输出 UNICODE_STRING
UNICODE_STRING uniString;
KdPrint("%wZ", &uniString);
VOID RtlInitAnsiString( _Out_ PANSI_STRING DestinationString, _In_opt_ PCSZ SourceString );
VOID RtlInitUnicodeString( _Out_ PUNICODE_STRING DestinationString, _In_opt_ PCWSTR SourceString );
#define BUFFER_SIZE 1024
UNICODE_STRING UnicodeString1 = {0};
//设置缓冲区大小
UnicodeString1.MaximumLength = BUFFER_SIZE;
//分配内存
UnicodeString1.Buffer = (PWSTR)ExAllocatePool(PagedPool,BUFFER_SIZE);
WCHAR* wideString = L"hello";
//设置字符长度,因为是宽字符,所以是字符长度的2倍
UnicodeString1.Length = 2*wcslen(wideString);
//保证缓冲区足够大,否则程序终止
ASSERT(UnicodeString1.MaximumLength>=UnicodeString1.Length);
//内存拷贝,
RtlCopyMemory(UnicodeString1.Buffer,wideString,UnicodeString1.Length);
//设置字符长度
UnicodeString1.Length = 2*wcslen(wideString);
KdPrint(("UnicodeString:%wZ\n",&UnicodeString1));
//清理内存
ExFreePool(UnicodeString1.Buffer);
UnicodeString1.Buffer = NULL;
UnicodeString1.Length = UnicodeString1.MaximumLength = 0;
最后一步清理内存,可以使用DDK函数简化:
RtlFreeAnsiString
RtlFreeUnicodeString
VOID RtlCopyString(
_Out_ PSTRING DestinationString,
_In_opt_ const STRING *SourceString
);
VOID RtlCopyUnicodeString( _Inout_ PUNICODE_STRING DestinationString, _In_opt_ PCUNICODE_STRING SourceString );
LONG RtlCompareString(
_In_ const STRING *String1,
_In_ const STRING *String2,
_In_ BOOLEAN CaseInSensitive
);
LONG RtlCompareUnicodeString(
_In_ PCUNICODE_STRING String1,
_In_ PCUNICODE_STRING String2,
_In_ BOOLEAN CaseInSensitive
);
BOOLEAN RtlEqualString(
_In_ const STRING *String1,
_In_ const STRING *String2,
_In_ BOOLEAN CaseInSensitive
);
BOOLEAN RtlEqualUnicodeString(
_In_ PCUNICODE_STRING String1,
_In_ PCUNICODE_STRING String2,
_In_ BOOLEAN CaseInSensitive
);
VOID RtlUpperString(
_Inout_ PSTRING DestinationString,
_In_ const STRING *SourceString
);
NTSTATUS RtlUpcaseUnicodeString(
_Inout_ PUNICODE_STRING DestinationString,
_In_ PCUNICODE_STRING SourceString,
_In_ BOOLEAN AllocateDestinationString
);
NTSTATUS RtlUnicodeStringToInteger(
_In_ PCUNICODE_STRING String,
_In_opt_ ULONG Base,
_Out_ PULONG Value
);
NTSTATUS RtlIntegerToUnicodeString(
_In_ ULONG Value,
_In_opt_ ULONG Base,
_Inout_ PUNICODE_STRING String
);
NTSTATUS RtlUnicodeStringToAnsiString(
_Inout_ PANSI_STRING DestinationString,
_In_ PCUNICODE_STRING SourceString,
_In_ BOOLEAN AllocateDestinationString
);
NTSTATUS RtlAnsiStringToUnicodeString(
_Inout_ PUNICODE_STRING DestinationString,
_In_ PCANSI_STRING SourceString,
_In_ BOOLEAN AllocateDestinationString
);