win10驱动开发12——字符串

字符串详细操作
ring3 char 字符数组 传入ring0后 转化为UNICODE_STRING处理

描述ANSI_STRINGUNICODE_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;
}

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值