03-驱动中的字符串操作

在内核层编程一般不使用Unicode而是使用UNICODE_STRING,(字符串长度以Length为准,不要去自己计算,未必是以’\0’结尾的,以免出错,都提供了还自己算它干嘛)
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
USHORT * Buffer;
PWCH Buffer;
} UNICODE_STRING;
字符串初始化:常用的初始化函数
RtlInitUnicodeString(
Out PUNICODE_STRING DestinationString,
In_opt_z __drv_aliasesMem PCWSTR SourceString
);
参数1:返回类型,表示需要初始化的UNICODE_STRING结构体
参数2:用来初始化的WCHAR*类型的字符串

微软还定义了RTL_CONSTANT_STRING(L“字符串”)宏可以快速的初始化字符串
RtlInitEmptyUnicodeString(目标字符串,源字符串,长度)Unicode字符串
RtlInitEmptyAnsiString(目标字符串,源字符串,长度)ACSII字符串

复制目标字符串:
RtlCopyUnicodeString(目标字符串,源字符串)Unicode字符串
RtlCopyString(目标字符串,源字符串)ACSII字符串
字符串操作API过多,以下函数为网络上摘录
// 内存操作
void MemoryOperation() {
PCHAR pcstr;
DbgPrint(“1.内存操作:\n”);
// 1)申请内存
pcstr = (PCHAR)ExAllocatePoolWithTag(NonPagedPool, 1024, ‘abcd’);
if (pcstr == NULL) {
DbgPrint(“内存分配失败\n”);
return;
}
// 2)清空内存
RtlZeroMemory(pcstr, 1024);

// 3)赋值内存
strcpy(pcstr, "这是一次内存测试");
DbgPrint("%s\n", pcstr);

// 4)释放内存
ExFreePoolWithTag(pcstr, 'abcd');
DbgPrint("**************\n");

}

// 使用宏来进行字符串初始化操作
void InitStringByRtl() {
DbgPrint(“2.通过宏来进行字符串初始化操作:\n”);
UNICODE_STRING us = RTL_CONSTANT_STRING(L"RTL_CONSTANT_STRING UnicodeString");
ANSI_STRING as = RTL_CONSTANT_STRING(“RTL_CONSTANT_STRING AnsiString”);
DbgPrint("%wZ\n", &us);
DbgPrint("%Z\n", &as);
DbgPrint("**************\n");
}

// 字符串赋值操作
void StringCopy() {
DbgPrint(“3.字符串复制操作:\n”);

// 1)声明目标字符串
UNICODE_STRING usDest;
ANSI_STRING asDest;
WCHAR wcstr[128] = { 0 }; // UnicodeString的缓冲区
CHAR asstr[128] = { 0 }; // AnsiString的缓冲区

// 2)初始化原字符串
UNICODE_STRING us = RTL_CONSTANT_STRING(L"UnicodeString");
ANSI_STRING as = RTL_CONSTANT_STRING("AnsiString");

// 3)初始化目标字符串(自定义缓冲区)
RtlInitEmptyUnicodeString(&usDest, wcstr, sizeof(wcstr));
RtlInitEmptyAnsiString(&asDest, asstr, sizeof(asstr));

// 4)复制目标字符串
RtlCopyUnicodeString(&usDest, &us);
RtlCopyString(&asDest, &as);

// 5)输出字符串
DbgPrint("%wZ\n", &usDest);
DbgPrint("%Z\n", &asDest);
DbgPrint("**************\n");

}

// 字符串比较操作
VOID CompareString() {
DbgPrint(“4.字符串比较操作:\n”);

// 1)初始化字符串
UNICODE_STRING s1 = RTL_CONSTANT_STRING(L"Hello world!");
UNICODE_STRING s2 = RTL_CONSTANT_STRING(L"Hello world!");

// 2)字符串比较
int ret = RtlCompareUnicodeString(&s1, &s2, TRUE);

// 3)输出字符串比较结果
if (ret == 0)
{
	KdPrint(("s1=s2\n"));
}
else if (ret < 0)
{
	KdPrint(("s1<s2\n"));
}
else
{
	KdPrint(("s1>s2\n"));
}
DbgPrint("**************\n");

}

// 字符串转换为写操作
void UpperString() {

DbgPrint("5.将字符串转换为大写:\n");

// 1)声明并初始化字符串
UNICODE_STRING us, usDest;
RtlInitUnicodeString(&us, L"abcd");
WCHAR wcstr[128] = { 0 };
RtlInitEmptyUnicodeString(&usDest, wcstr, sizeof(wcstr));

// 2)将字符串转换为大写
RtlUpcaseUnicodeString(&usDest, &us, FALSE);
DbgPrint("转换为大写:%wZ\n", &usDest);
DbgPrint("**************\n");

}

// 字符串与数字的转换
void StringToInteger() {
DbgPrint(“6.字符串与数字的转换:\n”);

UNICODE_STRING us, usDest;
int Value;
WCHAR wcstr[128] = { 0 };
RtlInitEmptyUnicodeString(&usDest, wcstr, sizeof(wcstr));

// 1)字符串转换为数字
RtlInitUnicodeString(&us, L"-123");
RtlUnicodeStringToInteger(&us, 10, &Value);
KdPrint(("%d\n", Value));

// 2)数字转换为字符串
RtlIntegerToUnicodeString(123, 10, &usDest);
KdPrint(("%wZ\n", &us));
DbgPrint("**************\n");

}

// 格式化输出
void FormatPrint() {
DbgPrint(“7.标准化输出:\n”);

// 1)初始化字符串
UNICODE_STRING str1 = RTL_CONSTANT_STRING(L"abc");
UNICODE_STRING us;
WCHAR wcstr[128] = { 0 };
RtlInitEmptyUnicodeString(&us, wcstr, sizeof(wcstr));

// 2)标准化格式输出 ntstrsafe.h 
RtlUnicodeStringPrintf(&us, L"%d%wZ\n", 10, &str1);

// 3)输出
DbgPrint("%wZ", &us);

DbgPrint("**************\n");

}

// ANSI与UnicodeString之间的转换
void AnsiToUnicode() {
DbgPrint(“8.ANSI与UnicodeString之间的转换:\n”);
// 1)初始化Unicode字符串
UNICODE_STRING us;
WCHAR wcstr[128] = { 0 };
RtlInitEmptyUnicodeString(&us, wcstr, sizeof(wcstr));

// 2)初始化ANSI字符串
ANSI_STRING as = RTL_CONSTANT_STRING("Hello World!");

// 3)ANSI转换为Unicode
RtlAnsiStringToUnicodeString(&us, &as, FALSE);
DbgPrint("%wZ\n", &us);
DbgPrint("**************\n");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值