驱动程序中字符串操作涉及到ASCII字符串、宽字符串,还有DDK定义的ANSI_STRING数据结构和UNICODE_STRING数据结构。

1)ASCII字符串和宽字符串

在应用程序中使用两种字符:一是char型字符串,负责记录ANSI字符集,它是指向一个char数组的指针,每个char型变量大小是一个字节,字符串是以0标志字符串结束的;一是wchar_t型的宽字符串,负责描述unicode字符集,它是指向一个wchar_t数组的指针,wchar_t字符大小为两个字节,字符串以0标志字符串结束。

ANSI字符构造如下:

char *str1 = "ASCE";

UNICODE字符构造如下:

wchar_t *str2 = L"ASCE";

(注:在构造字符串时使用关键字“L”,编译器会自动生成所需要的宽字符)

在驱动开发中,DDK将char和wchar_t替换成CHAR和WCHAR。驱动程序中使用KdPrint宏打印ASCII字符串和宽字符串:

CHAR *string1 = "ASCE";

KdPrint(("%s/n", string1));   //注意是小写%s

WCHAR *string2 = L"ASCE";

KdPrint(("%S/n", string2));  //注意是大写%S

2)ANSI_STRING字符串与UNICODE_STRING字符串

DDK不鼓励程序员使用C语言的字符串,主要是因为标准C字符串处理函数容易导致缓冲区溢出等错误。应该使用DDK自定义的字符串:

typedef struct _STRING {

USHORT Length; //字符的长度,单位是字节

USHORT MaximumLength; //整个字符串缓冲区的最大长度

PCHAR  Buffer; //缓冲区的指针

} ANSI_STRING, *PANSI_STRING;

这个结构对ASCII字符串进行封装。

和标准C字符串不同,STRING字符串不是以0标志字符结束的。标准C字符串中,如果缓冲区长度是N,则只能容纳N-1个字符的字符串,最后一个字节存储NULL;而在STRING字符串中,缓冲区大小是MaximumLength,最大字符串长度可以是MaximumLength,而不是MaximumLength-1。

与ANSI_STRING相对应,DDK将宽字符串封装成UNICODE_STRING数据结构:

typedef struct _UNICODE_STRING {

USHORT Length; //字符的长度,单位是字节。如果是N个字符,那么Length等于N的2倍

USHORT MaximumLength; //整个字符串缓冲区的最大长度,单位是字节

PWSTR  Buffer; //缓冲区的指针

} UNICODE_STRING, *PUNICODE_STRING;

与ANSI_STRING一样,UNICODE_STRING字符串不是以NULL为结束标志的。

打印这两种字符串方法如下:

ANSI_STRING ansiString;

//此处略去对ansiString的初始化

KdPrint(("%Z/n", &ansiString)); //注意是%Z

UNICODE_STRING uniString;

//此处略去对uniString的初始化

KdPrint(("%wZ/n", &uniString));//注意是%wZ

3)字符的初始化和销毁

ANSI_STRING字符串和UNICODE_STRING字符串使用前需要进行初始化,有两种方法构造这个数据结构:

(1)使用DDK提供的函数:

初始化ANSI_STRING字符串:

VOID RtlInitAnsiString(

__out     PANSI_STRING DestinationString, //要初始化的ANSI_STRING字符串

__in_opt  PCSZ SourceString //字符串的内容

);

初始化UNICODE_STRING字符串:

VOID RtlInitUnicodeString(

__out     PUNICODE_STRING DestinationString, //要初始化的UNICODE_STRING字符串

__in_opt  PCWSTR SourceString //字符串的内容

);

这种初始化的优点是操作简单,用完后不用清理内存。但有一个问题,就是如果修改SourceString,同时会导致DestinationString字符发生变化:

ANSI_STRING ansiString;

CHAR *string = "asce";

//初始化ANSI_STRING字符串

RtlInitAnsiString(&ansiString, string);

KdPrint(("ansiString: %Z/n", &ansiString));

//改变string

string[0] = 'a';

string[1] = 's';

string[2] = 'c';

string[3] = 'e';

//改变string的同时ansiString也改变了

KdPrint(("ansiString: %Z/n", &ansiString));

(2)程序员自己申请内存,并初始化内存,当不用字符串时,需要回收字符串占用的内存:

#define BUFFER_SIZE 1024

UNICODE_STRING UnicodeString = {0};

//设置缓冲区大小

UnicodeString.MaximumLength = BUFFER_SIZE;

//分配内存

UnicodeString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);

WCHAR *wideString = L"ASCE";

//设置字符串长度,因为是宽字符,所以是字符长度的倍

UnicodeString.Length = 2*wcslen(wideString);

//保证缓冲区足够大,否则程序终止

ASSERT(UnicodeString.MaximumLength >= UnicodeString.Length);

//内存复制

RtlCopyString(UnicodeString.Buffer, wideString, UnicodeString.Length);

KdPrint(("UnicodeString: %wZ/n", &UnicodeString));

//清理内存

ExFreePool(UnicodeString.Buffer);

UnicodeString.Buffer = NULL;

UnicodeString.Length = UnicodeString.MaximumLength = 0;

对于最后一步清理内存,DDK给出了简化函数,分别是RtlFreeAnsiString和RtlFreeUnicodeString,这两个函数内部调用了ExFreePool去回收内存的。

4)字符串复制

DDK提供针对ANSI_STRING字符串和UNICODE_STRING字符串的复制字符串函数:

VOID RtlCopyString(

__out     PSTRING DestinationString, //目的字符串

__in_opt  const STRING *SourceString //源字符串

);

VOID RtlCopyUnicodeString(

__inout   PUNICODE_STRING DestinationString, //目的字符串

__in_opt  PCUNICODE_STRING SourceString //源字符串

);

下面代码说明了RtlCopyUnicodeString函数的使用:

#define BUFFER_SIZE 1024

//初始化UnicodeString1

UNICODE_STRING UnicodeString1;

RtlInitUnicodeString(&UnicodeString1, L"ASCE");

//初始化UnicodeString2

UNICODE_STRING UnicodeString2 = {0};

UnicodeString2.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);

UnicodeString2.MaximumLength = BUFFER_SIZE;

//将初始化UnicodeString1复制到UnicodeString2

RtlCopyUnicodeString(&UnicodeString2, &UnicodeString1);

//分别显示UnicodeString1和UnicodeString2

KdPrint(("UnicodeString1: %wZ/n", &UnicodeString1));

KdPrint(("UnicodeString2: %wZ/n", &UnicodeString2));

//销毁UnicodeString2,注意:UnicodeString1不用销毁

RtlFreeUnicodeString(&UnicodeString2);

5)字符串比较

DDK提供了对ANSI_STRING字符串和UNICODE_STRING字符串的相关比较函数:

LONG RtlCompareString(

__in  const STRING *String1, //要比较的第一个字符串

__in  const STRING *String2, //要比较的第二个字符串

__in  BOOLEAN CaseInSensitive //是否对大小写敏感

);

LONG RtlCompareUnicodeString(

__in  PCUNICODE_STRING String1, //要比较的第一个字符串

__in  PCUNICODE_STRING String2, //要比较的第二个字符串

__in  BOOLEAN CaseInSensitive //是否对大小写敏感

);

DDK同时提供了RtlEqualString和RtlEqualUnicodeString函数,返回为非零代表相等,零代表不相等:

BOOLEAN RtlEqualString(

__in  const STRING *String1,

__in  const STRING *String2,

__in  BOOLEAN CaseInSensitive

);

BOOLEAN RtlEqualUnicodeString(

__in  PCUNICODE_STRING String1,

__in  PCUNICODE_STRING String2,

__in  BOOLEAN CaseInSensitive

);

函数实例:

//初始化UnicodeString1

UNICODE_STRING UnicodeString1;

RtlInitUnicodeString(&UnicodeString1, L"ASCE");

//初始化UnicodeString2

UNICODE_STRING UnicodeString2;

RtlnitUnicodeString(&UnicodeString2, L"ASCE BOY");

//判断字符串是否相等

if(RtlEqualUnicodeString(&UnicodeString1, &UnicodeString2, TRUE))

{

KdPrint(("UnicodeString1 and UnicodeString2 are equal/n"));

}

else

{

KdPrint(("UnicodeString1 and UnicodeString2 are not euqal/n"));

}

6)字符串转化成大写

DDK提供的将ANSI_STRING字符串和UNICODE_STRING字符串转换成大写的函数如下:

VOID RtlUpperString(

__inout  PSTRING DestinationString, //目的字符串

__in     const STRING *SourceString //源字符串

);

NTSTATUS RtlUpcaseUnicodeString(

__inout  PUNICODE_STRING DestinationString, //目的字符串

__in     PCUNICODE_STRING SourceString, //源字符串

__in     BOOLEAN AllocateDestinationString //是否为目的字符串分配内存,

//目的字符串和源字符串可以是同一个字符串

);

实例代码:

//初始化UnicodeString1

UNICODE_STRING UnicodeString;

RtlInitUnicodeString(&UnicodeString, L"ASCE BOY");

//变化前

KdPrint(("UnicodeString: %wZ/n", &UnicodeString));

//转化成大写

RtlUpcaseUnicodeString(&UnicodeString, &UnicodeString, FALSE);

//变化后

KdPrint(("UnicodeString: %wZ/n", &UnicodeString));

7)字符串与整型数字相互转换

将UNICODE_STRING字符串转换成整数:

NTSTATUS RtlUnicodeStringToInteger(

__in      PCUNICODE_STRING String, //需要转换的字符串

__in_opt  ULONG Base, //转换的数的进制(如2、8、10、16)

__out     PULONG Value //需要转换的数字

);

将整数转换成UNICODE_STRING字符串:

NTSTATUS RtlIntegerToUnicodeString(

__in      ULONG Value, //需要转换的数字

__in_opt  ULONG Base, //转换的数的进制(2、8、10、16)

__inout   PUNICODE_STRING String //需要转换的字符串

);

实例代码如下:

#define BUFFER_SIZE 1024

//字符串转换成数字

UNICODE_STRING UnicodeString;

RtlInitUnicodeString(&UnicodeString, L"-100");

ULONG lNumber;

NTSTATUS nStatus = RtlUnicodeStringToInteger(&UnicodeString, 10, &lNumber);

if(NT_SUCCESS(nStatus))

{

KdPrint(("Conver to integer successfully/n"));

KdPrint(("Result : %d/n", lNumber));

}

else

{

KdPrint(("Conver to integer failed/n"));

}

//将数字转换成字符串

UNICODE_STRING UnicodeStringSec = {0};

UnicodeStringSec.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);

UnicodeStringSec.MaximumLength = BUFFER_SIZE;

nStatus = RtlIntegerToUnicodeString(200, 10, &UnicodeStringSec);

if(NT_SUCCESS(nStatus))

{

KdPrint(("Cover to string successfully/n"));

KdPrint(("Result : %wZ/n", &UnicodeStringSec));

}

else

{

KdPrint(("Conver to string failed/n"));

}

//销毁UnicodeStringSec,注意:UnicodeString不用销毁

RtlFreeUnicodeString(&UnicodeStringSec);

8)ANSI_STRING字符串与UNICODE_STRING字符串的转换

将UNICODE_STRING字符串转换成ANSI_STRING字符串:

NTSTATUS RtlUnicodeStringToAnsiString(

__inout  PANSI_STRING DestinationString, //需要被转换的字符串

__in     PCUNICODE_STRING SourceString, //需要转换的源字符串

__in     BOOLEAN AllocateDestinationString //是否需要对被转换字符串分配内存

);

将ANSI_STRING字符串转换成UNICODE_STRING字符串:

NTSTATUS RtlAnsiStringToUnicodeString(

__inout  PUNICODE_STRING DestinationString, //需要被转换的字符串

__in     PCANSI_STRING SourceString, //需要转换的源字符串

__in     BOOLEAN AllocateDestinationString //是否需要对被转换字符串分配内存

);

实例代码如下:

//将UNICODE_STRING字符串转换成ANSI_STRING字符串

UNICODE_STRING UnicodeString;

RtlInitUnicodeString(&UnicodeString, L"ASCE BOY");

ANSI_STRING AnsiString;

NTSTATUS nStatus = RtlUnicodeStringToAnsiString(&AnsiString,

&UnicodeString, TRUE);

if(NT_SUCCESS(nStatus))

{

KdPrint(("Conver successfully/n"));

KdPrint(("Result:%Z/n", &AnsiString));

}

else

{

KdPrint(("Conver failed/n"));

}

//销毁AnsiString

RtlFreeAnsiString(&AnsiString);

//将ANSI_STRING字符串转换成UNICODE_STRING字符串

ANSI_STRING AnsiStringSec;

RtlInitString(&AnsiStringSec, "ASCE BOY");

UNICODE_STRING UnicodeStringSec;

nStatus = RtlAnsiStringToUnicodeString(&UnicodeStringSec,

&AnsiStringSec, TRUE);

if(NT_SUCCESS(nStatus))

{

KdPrint(("Conver successfully/n"));

KdPrint(("Result: %wZ/n", &UnicodeStringSec));

}

else

{

KdPrint(("Conver failed/n"));

}

//销毁UnicodeStringSec

RtlFreeUnicodeString(&UnicodeStringSec);

Windows内核 字符串基本操作的更多相关文章

  1. Windows内核下操作字符串!

    * Windows内核下操作字符串! */ #include <ntddk.h> #include <ntstrsafe.h> #define BUFFER_SIZE 1024 ...

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

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

  3. Windows内核 基本数据结构

    驱动对象: 每个驱动程序都会有唯一的驱动对象与之对应,并且这个驱动对象是在驱动加载时被内核中的对象管理程序所创建的.驱动对象用DRIVER_OBJECT数据结构表示,它作为驱动的一个实例被内核加载,并 ...

  4. Windows内核安全与驱动开发

    这篇是计算机中Windows Mobile/Symbian类的优质预售推荐<Windows内核安全与驱动开发>. 编辑推荐 本书适合计算机安全软件从业人员.计算机相关专业院校学生以及有一定 ...

  5. windows内核编程之常用数据结构

    1.返回状态 绝大部分的内核api返回值都是一个返回状态,也就是一个错误代码.这个类型为NTSTATUS.我们自己写的函数也大部分这样做. NTSTATUS MyFunction() { NTSTAT ...

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

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

  7. 《天书夜读:从汇编语言到windows内核编程》五 WDM驱动开发环境搭建

    (原书)所有内核空间共享,DriverEntery是内核程序入口,在内核程序被加载时,这个函数被调用,加载入的进程为system进程,xp下它的pid是4.内核程序的编写有一定的规则: 不能调用win ...

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

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

  9. [转帖]Windows 内核说明

    来源:https://zhidao.baidu.com/question/398191459.html 自己的理解. windows 的内核文件 是在 c:\windows\system32 目录下面 ...

随机推荐

  1. 自定义鼠标光标,制作cur,设置热点,中心点。

    ..没哈好说的,,只是推荐一个软件 ArtCursor..非常好用. 关于另外一个更改光标的重要问题:鼠标的hotspot,也就是鼠标的作用点问题,本人关于这个问题纠结了很久,始终无法找到更改HCUR ...

  2. Android自动化测试 - MonkeyRunner(二) 锤子便签测试脚本

    来源于:http://testerhome.com/topics/878 # encoding=utf-8 #导入python中自带的time模块和sys模块,脚本中都要用到它们. import ti ...

  3. SolrCloud zookeeper节点信息

    shard 节点列表 get /collections/appstore/leaders/shard1 { "core":"appstore", "n ...

  4. BZOJ 2957 & 线段树上的查询

    题意: 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个二 ...

  5. hdu1710 Binary Tree Traversals(二叉树的遍历)

    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjo ...

  6. hdu1181 变形课

    Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个 ...

  7. Leetcode Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. NOIp 2013 #1 积木大赛 Label:有趣的模拟

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  9. 关于jQuery的inArray 方法介绍

    例如: 代码如下: $.get('aaaaa.ashx',null,function(d){ // 假设d 返回 的值为 1,3,43,23,54,67 var arr = d.split(','); ...

  10. URAL 1223. Chernobyl’ Eagle on a Roof

    题目链接 以前做过的一题,URAL数据强点,优化了一下. #include <iostream> #include <cstdio> #include <cstring& ...