*
Windows内核下操作字符串!
*/
#include <ntddk.h>
#include <ntstrsafe.h>
#define BUFFER_SIZE 1024 VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject)
{
KdPrint(("DriverUnload Load...\n"));
} //===========================================================================
// ANSI_STRING结构和UNICODE_STRING结构的使用
#pragma code_seg("INIT")
NTSTATUS StringTest(VOID)
{
ANSI_STRING AStString = { };
UNICODE_STRING UStString2 = { };
UNICODE_STRING UStString3 = RTL_CONSTANT_STRING(L"Initialization string directly!");
CHAR *SzHello = "hello";
WCHAR *WSzHello = L"hello"; // 初始化ANSI_STRING字符串的做法
RtlInitAnsiString(&AStString, SzHello);
// %Z打印ANSI的结构字符串
KdPrint(("StringTest->ANSI_STRING: %Z\n", &AStString)); SzHello[] = 'H';
SzHello[] = 'E';
SzHello[] = 'L';
SzHello[] = 'L';
SzHello[] = 'O';
SzHello[] = '\0'; // 改变SzHello, 结构也会发生改变, ANSI_STRING里面拥有的只是指针
KdPrint(("ANSI_STRING: %Z\n", &AStString)); // 手工初始化字符串
UStString2.MaximumLength = BUFFER_SIZE; // 最大缓冲区 // 分配内存, 在分页内存中
UStString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE); // 设置字符长度,因为是宽字符,所以是字符长度的2倍
UStString2.Length = * wcslen(WSzHello); // 保证缓冲区足够大, 否则程序终止
ASSERT(UStString2.MaximumLength >= UStString2.Length); // 内存COPY, Copy的长度就是Unicode字符串的长度
RtlCopyMemory(UStString2.Buffer, WSzHello, UStString2.Length); // 打印UnicodeString的方法%wZ
KdPrint(("StringTest->UStString2:%wZ\n", &UStString2));
KdPrint(("StringTest->UStString3:%wZ\n", &UStString3)); // 清理内存
ExFreePool(UStString2.Buffer);
UStString2.Buffer = NULL;
UStString2.Length = UStString2.MaximumLength = ; return STATUS_SUCCESS;
}
//===========================================================================
// 字符串测试2, 字符串之间的Copy
#pragma code_seg("INIT")
NTSTATUS StringCopyTest(VOID)
{
UNICODE_STRING UStString1 = { };
UNICODE_STRING UStString2 = { }; // RtlInitUnicodeString不用释放
RtlInitUnicodeString(&UStString1, L"Hello World"); // 这样也是可以初始化的, 不过要记得释放
UStString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE); // Copy字符串之前必须先填写最大长度
UStString2.MaximumLength = BUFFER_SIZE; // 将初始化UStString2拷贝到UStString1
RtlCopyUnicodeString(&UStString2, &UStString1); // 分别显示UnicodeString1和UnicodeString2
KdPrint(("StringCopyTest->UnicodeString1:%wZ!\n", &UStString1));
KdPrint(("StringCopyTest->UnicodeString2:%wZ!\n", &UStString2)); // 字符串的连接
RtlUnicodeStringCatUnicodeString(&UStString2, &UStString1);
KdPrint(("StringCopyTest->UnicodeString2:%wZ!\n", &UStString2)); // 销毁UnicodeString2 注意!!UnicodeString1不用销毁
RtlFreeUnicodeString(&UStString2); return STATUS_SUCCESS;
} //===========================================================================
// 字符串的比较试验
#pragma code_seg("INIT")
VOID StringCompareTest(VOID)
{
LONG dwFlags;
UNICODE_STRING UStString1;
UNICODE_STRING UStString2; RtlInitUnicodeString(&UStString1, L"Hello World");
RtlInitUnicodeString(&UStString2, L"Hello WorlD"); // 比较字符串, 区分大小写(FALSE), 相等返回True
if (RtlEqualUnicodeString(&UStString1, &UStString2, FALSE))
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are equal\n"));
}
else
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are NOT equal\n"));
} // 比较字符串, 不区分大小写(TRUE), 相等返回TRUE
RtlInitUnicodeString(&UStString2, L"Hello World");
if (RtlEqualUnicodeString(&UStString1, &UStString2, FALSE))
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are equal\n"));
}
else
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are NOT equal\n"));
} // 比较字符串, 不区分大小写(TRUE), 相等返回TRUE
RtlInitUnicodeString(&UStString2, L"Hello");
if (RtlEqualUnicodeString(&UStString1, &UStString2, TRUE))
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are equal\n"));
}
else
{
KdPrint(("StringCompareTest->UStString1 and UStString2 are NOT equal\n"));
} // 比较字符串大小, 不区分大小写(TRUE)
dwFlags = RtlCompareUnicodeString(&UStString1, &UStString2, TRUE);
if (dwFlags == )
{
KdPrint(("StringCompareTest->UStString1 == UStString2\n"));
}
else if(dwFlags > )
{
KdPrint(("StringCompareTest->UStString1 > UStString2\n"));
}
else
{
KdPrint(("StringCompareTest->UStString1 < UStString2\n"));
}
}
//=========================================================================== // 字符串转整数型试验
#pragma code_seg("INIT")
VOID StringToIntegerTest(VOID)
{
ULONG uValue;
NTSTATUS nStatus;
UNICODE_STRING UStString1 = RTL_CONSTANT_STRING(L"-100");
UNICODE_STRING UStString2={ }; // 转换正常的十进制数字
nStatus = RtlUnicodeStringToInteger(&UStString1, , &uValue);
if (NT_SUCCESS(nStatus))
{
KdPrint(("StringToIntegerTest->Conver to integer succussfully!\n"));
KdPrint(("Result:%d\n", uValue));
}
else
{
KdPrint(("StringToIntegerTest->Conver to integer unsuccessfully!\n"));
} // 转换16进制数据
RtlInitUnicodeString(&UStString1, L"");
nStatus = RtlUnicodeStringToInteger(&UStString1, , &uValue);
if (NT_SUCCESS(nStatus))
{
KdPrint(("StringToIntegerTest->Conver to integer succussfully!"));
KdPrint(("Result:%u 0x%X \n", uValue,uValue));
}
else
{
KdPrint(("StringToIntegerTest->Conver to integer unsuccessfully!\n"));
} // 数字转换成字符串, 初始化UnicodeString2
UStString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
UStString2.MaximumLength = BUFFER_SIZE;
nStatus = RtlIntegerToUnicodeString(, , &UStString2);
if (NT_SUCCESS(nStatus))
{
KdPrint(("StringToIntegerTest->Conver to string succussfully!"));
KdPrint(("Result:%wZ\n", &UStString2));
}
else
{
KdPrint(("StringToIntegerTest->Conver to string unsuccessfully!\n"));
} // 16进制数字转字符串
nStatus = RtlIntegerToUnicodeString(, , &UStString2);
if (NT_SUCCESS(nStatus))
{
KdPrint(("StringToIntegerTest->Conver to string succussfully!"));
KdPrint(("Result:%wZ\n", &UStString2));
}
else
{
KdPrint(("StringToIntegerTest->Conver to string unsuccessfully!\n"));
} // UStString2, 注意!!UStString1不用销毁
RtlFreeUnicodeString(&UStString2);
}
//=========================================================================== // 字符串变大写实验
#pragma code_seg("INIT")
VOID StringToUpperTest(VOID)
{
UNICODE_STRING UStString1 = RTL_CONSTANT_STRING(L"Hello World"); // 变化前
KdPrint(("StringToUpperTest->UnicodeString1:%wZ\n", &UStString1)); // 变大写
RtlUpcaseUnicodeString(&UStString1, &UStString1, FALSE); // 变化后
KdPrint(("StringToUpperTest->UnicodeString1:%wZ\n", &UStString1));
}
//=========================================================================== // ANSI_STRING字符串和UNICODE_STRING之间的转换
#pragma code_seg("INIT")
VOID StringConverTest(VOID)
{
ANSI_STRING StString1 = { };
UNICODE_STRING UStString2 = { };
ANSI_STRING StString2 = RTL_CONSTANT_STRING("Hello World");
UNICODE_STRING UStString1 = RTL_CONSTANT_STRING(L"Hello World"); // (1)将UNICODE_STRING字符串转换成ANSI_STRING字符串
NTSTATUS nStatus = RtlUnicodeStringToAnsiString(&StString1, &UStString1, TRUE);
if (NT_SUCCESS(nStatus))
{
KdPrint(("Conver succussfully!"));
KdPrint(("Result:%Z\n",&StString1));
}
else
{
KdPrint(("Conver unsuccessfully!\n"));
} // 销毁AnsiString1
RtlFreeAnsiString(&StString1); // (2)将ANSI_STRING字符串转换成UNICODE_STRING字符串初始化AnsiString2
nStatus = RtlAnsiStringToUnicodeString(&UStString2, &StString2, TRUE);
if (NT_SUCCESS(nStatus))
{
KdPrint(("Conver succussfully!\n"));
KdPrint(("Result:%wZ\n",&UStString2));
}
else
{
KdPrint(("Conver unsuccessfully!\n"));
}
// 销毁UnicodeString2
RtlFreeUnicodeString(&UStString2);
} NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pUSzReg)
{
pDriverObj->DriverUnload = DriverUnload;
StringTest(); // 字符串初始化试验
StringCopyTest(); // 字符串Copy试验
StringCompareTest(); // 字符串的比较试验
StringToIntegerTest(); // 字符串转整数型试验
StringToUpperTest(); // 字符串变大写实验
StringConverTest(); // ANSI和UNICODE之间的转换 return STATUS_SUCCESS;
}

Windows内核下操作字符串!的更多相关文章

  1. Win64 驱动内核编程-4.内核里操作字符串

    内核里操作字符串 字符串本质上就是一段内存,之所以和内存使用分开讲,是因为内核里的字符串太有花 样了,细数下来竟然有 4 种字符串!这四种字符串,分别是:CHAR*.WCHAR*.ANSI_STRIN ...

  2. windows 内核下获取进程路径

    windows 内核下获取进程路径 思路:1):在EPROCESS结构中获取.此时要用到一个导出函数:PsGetProcessImageFileName,申明如下: NTSYSAPI UCHAR *  ...

  3. Java在linux环境下和windows环境下日期字符串显示不同

    图片如果损坏,点击链接: https://www.toutiao.com/i6511565147322974724/ 出现的现象: 在Java中我想要将当前的时间格式化为需要的字符串,然后存放到数据库 ...

  4. 《Windows内核安全与驱动开发》 3.1 字符串操作

    <Windows内核安全与驱动开发>阅读笔记 -- 索引目录 <Windows内核安全与驱动开发> 3.1 字符串操作 一.字符串的初始化 1. 判断下列代码为什么会蓝屏? U ...

  5. Windows内核驱动中操作文件

    本页主题:如何在windows内核驱动中对文件操作,实现对文件的拷贝.粘贴.删除.查询信息等,这是很常用也是很简单的方法. 部分内容参考:http://www.cppblog.com/aurain/a ...

  6. 【windwos 操作系统】关键的Windows内核数据结构一览(下)

    I/O管理器 nt!_IRP IRP表示一个I/O请求包结构体,它用来封装执行一个特定I/O操作所需要的所有参数以及I/O操作的状态.IRP的表现也类似于一个线程独立调用栈因此它可以从一个线程传递到另 ...

  7. 《天书夜读:从汇编语言到windows内核编程》八 文件操作与注册表操作

    1)Windows运用程序的文件与注册表操作进入R0层之后,都有对应的内核函数实现.在windows内核中,无论打开的是文件.注册表或者设备,都需要使用InitializeObjectAttribut ...

  8. [原创]使用GCC创建 Windows NT 下的内核DLL

    原文链接:使用GCC创建 Windows NT 下的内核DLL 在温习<<Windows 2000 Driving>>分层驱动程序一章的时候,看到了关于紧耦合驱动连接方式,这种 ...

  9. windows环境下protobuf的java操作{编译,序列化,反序列化}

    google protocol buffer的使用和原理 概况: Protocol Buffers(也就是protobuf)是谷歌的语言中立的.平台中立的.可扩展的用于序列化结构化的数据: windo ...

随机推荐

  1. Dual Palindromes

    Dual PalindromesMario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same ...

  2. 交叉编译php5,、nginx、squid方法

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 交叉编译php5 软件版本:php-5.4.27 依赖库:zlib,libxml2 交叉编译器:arm-hisi ...

  3. 破解TP-Link路由-嗅探PPPoE拨号密码

    如果你平时都使用路由器直接上网,那么你还记得你的宽带(ADSL)帐户名和密码吗?忘记密码后又该如何找回呢?别急,本文带你一同找回遗忘的ADSL密码.1.安全性较差的路由器(例如腾达的某些路由器):这里 ...

  4. Ubuntu 14.04/14.10下安装VMware Workstation 11图文教程

    VMware workstation 是一个可以进行桌面操作的虚拟软件.它可以让我们在一台电脑或者虚拟机中运行多个虚拟机. 由VMware公司研发和维护.由于是商业软件,我们需要买他们家的许可证或者说 ...

  5. linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 )

    在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略 ...

  6. MVC3和MVC4相关问题

    从TFS获取的MVC项目生成一直提示这个错误,我的VS中MVC3和MVC4都有,TFS项目中的MVC版本应该是MVC3的,现在我要用这个项目,但是一直是这个错误,请高手指点,积分不多了,见谅 程序集“ ...

  7. Ubuntu 用户安装 MATE

      MATE 是经典桌面 Gnome 2 的分支,该桌面按照 Windows 用户操作习惯设计,适合于 Windows 转投 Linux 的初级用户,MATE 做了功能改进和新增功能.如:增加窗口管理 ...

  8. Java 全半角转换

    * 全角转半角的 转换函数* @return String*/public static final String full2HalfChange(String QJstr){StringBuffer ...

  9. VI和VIM编辑器深入学习笔记--基本vi命令

    双十一过后有点闲,找本书给这段时间碰到的一些问题充充电,先从linux vi命令开始: 移动光标: “h” 向左一个字符,“j”向下一行,“k” 向上一行,“l” 向右一个字符(虽然我们可以用方向键, ...

  10. JDK JRE 区别

    JDK  包含了编译器,比如让.java编译成.classs文件. JRE =Java Runtime Environment j是一些比如一些split函数需要的包,都在里面,基本的运行环境都在JR ...