void StrAllcateTest() { KdPrint(("----------------------字符串分配内存----------------------/n")); ANSI_STRING AnsiString = {0}; UNICODE_STRING UnicodeString = {0}; //AnsiString.Buffer = "Ansi字符串"; AnsiString.Buffer = (PCHAR)ExAllocatePool(PagedPool,512); strcpy(AnsiString.Buffer,"Ansi字符串"); AnsiString.Length = strlen(AnsiString.Buffer); AnsiString.MaximumLength = 512; //UnicodeString.Buffer = L"Unicode字符串"; UnicodeString.Buffer = (PWSTR)ExAllocatePool(PagedPool,512); wcscpy(UnicodeString.Buffer,L"Unicode字符串"); UnicodeString.Length = wcslen(UnicodeString.Buffer)*2; //宽字符要乘2 UnicodeString.MaximumLength = 512; //输出Ansi字符串用 %Z KdPrint(("Ansi字符串内核内存地址:%x -- %Z",&AnsiString,&AnsiString)); //输出Unicode字符串用%wZ KdPrint(("Unicode字符串内核内存地址:%x -- %wZ",&UnicodeString,&UnicodeString)); //释放 RtlFreeAnsiString(&AnsiString); RtlFreeUnicodeString(&UnicodeString); } void StrCopyAndCompare() { _asm int 3 KdPrint(("----------------------宽字符拷贝----------------------/n")); UNICODE_STRING UnicodeString1 = {0},UnicodeString2= {0}; //需要先给UnicodeString2分配一块内存 UnicodeString2.Buffer = (PWSTR)ExAllocatePool(PagedPool,512); UnicodeString2.MaximumLength = 512; RtlInitUnicodeString(&UnicodeString1,L"UnicodeString1"); RtlCopyUnicodeString(&UnicodeString2,&UnicodeString1); KdPrint(("----------------------宽字符比较----------------------/n")); if(RtlEqualUnicodeString(&UnicodeString1,&UnicodeString2,TRUE)) KdPrint(("字符串相等!")); else KdPrint(("字符串不相等!")); RtlFreeUnicodeString(&UnicodeString2); } void StrToUpper() { KdPrint(("----------------------宽字符大小转换----------------------/n")); _asm int 3 UNICODE_STRING UnicodeString1; int UnicodeInt; RtlInitUnicodeString(&UnicodeString1,L"UnicodeString"); KdPrint(("UnicodeString 转换前:%wZ",UnicodeString1)); //参数1 输出的指针 参数2:需要转换的指针 RtlUpcaseUnicodeString(&UnicodeString1,&UnicodeString1,false); KdPrint(("UnicodeString 转换后:%wZ",UnicodeString1)); } void StrToint() { KdPrint(("----------------------宽字符和整型转换----------------------/n")); _asm int 3; UNICODE_STRING UnicoeString; UNICODE_STRING UnicodeString2={0}; ULONG UnicodeInt; NTSTATUS status; //返回的状态 ULONG UnicodeInt2 = 399; RtlInitUnicodeString( &UnicoeString,L"500"); KdPrint(("UnicodeString转Int前:%wZ",UnicoeString)); //参数2为进制.... RtlUnicodeStringToInteger(&UnicoeString,10,&UnicodeInt); KdPrint(("UnicodeString转Int后:%d",UnicodeInt)); //数字转换成字符串 PagedPool = 1 UnicodeString2.Buffer = (PWSTR)ExAllocatePool(PagedPool,512); UnicodeString2.Length = wcslen(UnicodeString2.Buffer) * 2; UnicodeString2.MaximumLength = 512; //最大的存储空间; status = RtlIntegerToUnicodeString(UnicodeInt2,10,&UnicodeString2); if(!NT_SUCCESS(status)) { KdPrint(("转换失败!")); } } void UnicodeToAnsi() { KdPrint(("----------------------Unicode转换Ansi----------------------/n")); _asm int 3; UNICODE_STRING UnicodeString = {0}; UNICODE_STRING UnicodeString2= {0}; ANSI_STRING AnsiString = {0}; ANSI_STRING AnsiString2 = {0}; RtlInitUnicodeString(&UnicodeString,L"Unicode字符转换"); //参数3:是否分配内存 RtlUnicodeStringToAnsiString(&AnsiString, &UnicodeString,TRUE); KdPrint(("转换后的AnsiString:%Z",AnsiString)); RtlInitAnsiString(&AnsiString2,"AnsiString"); RtlAnsiStringToUnicodeString( &UnicodeString2, &AnsiString2, TRUE); KdPrint(("转换后的UnicodeString:%wZ",UnicodeString2)); }