字符串详细操作
ring3 char 字符数组 传入ring0后 转化为UNICODE_STRING处理
描述 | ANSI_STRING | UNICODE_STRING |
---|---|---|
初始化 | RtlInitAnsiString() RtlInitEmptyAnsiString() RTL_CONSTANT_STRING | RtlInitUnicodeString() RtlInitEmptyUnicodeString() RTL_CONSTANT_STRING |
复制 | RtlCopyString() | RtlCopyUnicodeString() |
比较 | RtlCompareString() | RtlCompareUnicodeString() |
转成大写 | RtlUpperString() | RtlUpcaseUnicodeString() |
字符串与整数互转换 | RtlIntegerToUnicodeString() RtlUnicodeStringToInteger() | |
ANSI_STRING与UNICODE_STRING相互转换 | RtlAnsiStringToUnicodeString() | RtlUnicodeStringToAnsiString() |
字符串初始化
方式一
#include <ntddk.h>
VOID Unload(IN PDRIVER_OBJECT pDriverObject)
{
//驱动卸载的时候显示
KdPrint(("Goodbye driver\n"));
}
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
//字符串初始化方式一
//参数1:长度初始化;参数2:占用内存初始化;参数3:字符串(L代表unicode字符串);
UNICODE_STRING us = { (USHORT)(wcslen(L"hello world") * sizeof(WCHAR)),(USHORT)(wcslen(L"hello world") * sizeof(WCHAR) + 2),L"hello world" };
ANSI_STRING as = { (USHORT)(strlen("hello world") * sizeof(CHAR)),(USHORT)(strlen("hello world") * sizeof(CHAR) + 1),"hello world" };
KdPrint(("Hello driver\n"));
DriverObject->DriverUnload = Unload;
KdPrint(("%wZ\n", &us));
KdPrint(("%Z\n", &as));
//字符串初始化方式二与方式一对等
us =RTL_CONSTANT_STRING(L"hello1 world");
as = RTL_CONSTANT_STRING("hello1 world");
KdPrint(("%wZ\n", &us));
KdPrint(("%Z\n", &as));
//方式三
RtlInitUnicodeString(&us, L"hello2 world");
RtlInitAnsiString(&as, "hello2 world");
KdPrint(("%wZ\n", &us));
KdPrint(("%Z\n", &as));
//方式四 堆内存函数初始化
us.Length = 0;
us.MaximumLength = 1024;
us.Buffer = (PWCH)ExAllocatePoolWithTag(PagedPool, 1024,'a');
//方式五 堆内存函数初始化
as.Length = 0;
CHAR cstr[1024] = {0};
as.MaximumLength = sizeof(cstr);
as.Buffer = cstr;
//方式六与方式五对等
RtlInitEmptyAnsiString(&as, cstr, sizeof(cstr));
WCHAR wcstr[1024] = { 0 };
//=================复制=============
UNICODE_STRING us1 = RTL_CONSTANT_STRING(L"hello3 world");
ANSI_STRING as1 = RTL_CONSTANT_STRING("hello3 world");
UNICODE_STRING usDest;
ANSI_STRING asDest;
RtlInitEmptyUnicodeString(&usDest, wcstr,sizeof(wcstr));
RtlInitEmptyAnsiString(&asDest, cstr, sizeof(cstr));
RtlCopyUnicodeString(&usDest, &us1);
RtlCopyString(&asDest, &as1);
/*=========================*/
RtlInitUnicodeString(&us1, L"这是Unicodestring");
RtlUnicodeStringToAnsiString(&asDest, &us1,FALSE);
KdPrint(("%Z\n", &asDest));
/*=========================*/
RtlInitAnsiString(&as1, "这是ansistring");
RtlAnsiStringToUnicodeString(&usDest, &as1, FALSE);
KdPrint(("%wZ\n", &usDest));
/*=========================*/
RtlInitUnicodeString(&us1, L"驱动");
RtlUnicodeStringPrintf(&usDest, L"%d= %wZ= %Z= %s= %S= ",10,&us1,&asDest,L"hello","world");
KdPrint(("%wZ\n",&usDest));
/*=========================*/
KdPrint(("%wZ\n", &asDest));
KdPrint(("%wZ\n", &usDest));
KdPrint(("%Z\n", &asDest));
//ture 不区分大小写 //false区分
LONG ret=RtlCompareString(&asDest, &as1,TRUE);
if (ret==0)
{
KdPrint(("asDest=as1"));
}
else if (ret<0)
{
KdPrint(("asDest<as1"));
}
else if (ret > 0)
{
KdPrint(("asDest>as1"));
}
return STATUS_SUCCESS;
}