Windows简单驱动编程(一):内核中常见的字符串操作

开发环境:win10+vs2019+wdk10+vm_win7_x86

本文内容:介绍内核中常见的字符串操作,字符串定义,字符串初始化,字符串复制,连接,比较。

说明:环境部分大家自己装,网上教程很多,这里就不重复了。由于自己内核,驱动这方面知识缺乏,怕工作中偶尔碰见这样的病毒,绕不过的,只得学!大家加油!直接贴代码,注释我都标好了。

#include <ntddk.h>

#define BUFFER_SIZE 256

//驱动卸载函数,不写的话驱动无法卸载,只能重启卸载
NTSTATUS Unload(PDRIVER_OBJECT driver)
{
	DbgPrint("driver unload success.....\n");
	return STATUS_SUCCESS;
}


//入口点,类似于mian(),双击调试时windbg可以下断点:driver_name!DriverEntry,就可以断在这里了
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
	//内核调试输出,类似于printf,DbgPrint和KdPrint一样,注意参数,KdPrint在发布版会被优化掉
	/*
	DbgPrint("this is my first driver.....\n");
	*/

	//下面是内核中的字符串类型,内核中常用的是unicode类型的字符串,使用时得用RtlInitUnicodeString()函数初始化,宽字符长度前面加个L
	/*
	CHAR* char_string = "hello CHAR";
	WCHAR* wchar_string = L"hello WCHAR";
	UNICODE_STRING unicode_string;
	RtlInitUnicodeString(&unicode_string, L"hello unicode string");
	KdPrint(("%s", char_string));
	KdPrint(("%S", wchar_string));
	KdPrint(("%wZ", &unicode_string));
	*/

	//下面是字符串的常见操作
	//1.字符串复制,记得初始化目标buffer
	/*
	UNICODE_STRING source_string;
	UNICODE_STRING dest_string;
	RtlInitUnicodeString(&source_string, L"this is source string");
	//分配内存
	dest_string.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
	dest_string.MaximumLength = BUFFER_SIZE;
	//复制到目标buffer
	RtlCopyUnicodeString(&dest_string, &source_string);
	KdPrint(("dest string is :%wZ", &dest_string));
	//记得释放开辟的buffer
	RtlFreeUnicodeString(&dest_string);
	*/

	//2.字符串连接,使用RtlAppendUnicodeToString()函数
	/*
	UNICODE_STRING source_string;
	UNICODE_STRING dest_string;
	RtlInitUnicodeString(&source_string, L"hello wait append : ");
	dest_string.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
	dest_string.MaximumLength = BUFFER_SIZE;
	RtlCopyUnicodeString(&dest_string, &source_string);

	if (STATUS_BUFFER_TOO_SMALL == RtlAppendUnicodeToString(&dest_string, L"append string"))
	{
		KdPrint(("STATUS_BUFFER_TOO_SMALL"));
	}
	KdPrint(("%wZ", &dest_string));
	RtlFreeUnicodeString(&dest_string);
	*/
	
	//3.字符串比较,使用RtlCompareUnicodeString()函数
	/*
	UNICODE_STRING compare_string_1;
	UNICODE_STRING compare_string_2;
	int return_num = 0;
	RtlInitUnicodeString(&compare_string_1, L"I am string 1");
	RtlInitUnicodeString(&compare_string_2, L"I am string 2");

	//TRUE:区分大小写,FALSE:不区分大小写
	return_num = RtlCompareUnicodeString(&compare_string_1, &compare_string_2, TRUE);
	if (return_num == 0)
	{
		KdPrint(("字符串1等与字符串2\n"));
	}
	if (return_num > 0)
	{
		KdPrint(("字符串1大于字符串2\n"));
	}
	if (return_num < 0)
	{
		KdPrint(("字符串1小于字符串2\n"));
	}
	*/

	//定义驱动卸载函数
	driver->DriverUnload = Unload;
	return STATUS_SUCCESS;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值