驱动开发(5)内核中的字符串

来源:互联网 发布:javascript面向对象 编辑:程序博客网 时间:2024/06/04 19:26

本博文由CSDN博主zuishikonghuan所作,版权归zuishikonghuan所有,转载请注明出处:http://blog.csdn.net/zuishikonghuan/article/details/49717715

在驱动开发中,内核函数使用的字符串不再是应用程序使用的Win32子系统API和Native API中的char*和wchar_t*,而是内核Unicode字符串UNICODE_STRING。

内核字符串有两种,ANSI字符串是ANSI_STRING,Unicode字符串是UNICODE_STRING,他们的结构是这样定义的:

typedef struct _STRING {  USHORT Length;  USHORT MaximumLength;  PCHAR  Buffer;} ANSI_STRING, *PANSI_STRING;
typedef struct _UNICODE_STRING {  USHORT Length;  USHORT MaximumLength;  PWSTR  Buffer;} UNICODE_STRING, *PUNICODE_STRING;

Length:字符串长度,都是以字节数为单位的!而不是字符数。

MaximumLength:字符串的最大长度,即 Buffer 指向的缓冲区长度,字节为单位。

Buffer:真实字符串的指针。

下面,以Unicode版本,介绍一下内核字符串的创建、释放、复制、比较等

创建字符串:RtlInitUnicodeString

VOID RtlInitUnicodeString(  _Out_    PUNICODE_STRING DestinationString,  _In_opt_ PCWSTR          SourceString);
DestinationString:指向要初始化的UNICODE_STRING结构的指针。

SourceString:一个指向以 null 结尾的宽字符字符串,用于初始化DestinationString。

释放字符串:RtlFreeUnicodeString

VOID RtlFreeUnicodeString(  _Inout_ PUNICODE_STRING UnicodeString);
UnicodeString:指向以前分配的、要释放的 Unicode 字符串UNICODE_STRING结构的指针。

复制字符串:RtlCopyString

VOID RtlCopyString(  _Out_          PSTRING DestinationString,  _In_opt_ const STRING  *SourceString)
DestinationString:指向目标字符串的指针。

SourceString:指向源字符串的指针。

注:
1。这里的字符串ANSI_STRING结构和UNICODE_STRING结构均可。
2。目标字符串结构中的缓冲区一定要事先申请好,从 SourceString 复制的字节数是 SourceString 的长度或 DestinationString 的最大长度,以较小者为准。

字符串比较:RtlCompareUnicodeString

LONG RtlCompareUnicodeString(  _In_ PCUNICODE_STRING String1,  _In_ PCUNICODE_STRING String2,  _In_ BOOLEAN          CaseInSensitive);
String1:指向第一个字符串UNICODE_STRING结构的指针

String2:指向第二个字符串UNICODE_STRING结构的指针。

CaseInSensitive:如果为 TRUE,则在做比较时应忽略大小写。

返回值:RtlCompareUnicodeString 返回比较的结果:
0:String1 等于 String2。
<0:String1 小于 String2。
>0:String1 大于 String2。

另外还有几个比较常用的,但在这里不再详细说了,都很简单。

字符串编码转换:RtlAnsiStringToUnicodeString、 RtlUnicodeStringToAnsiString
字符串转换到大写:RtlUpperString
字符串与int的转换:RtlUnicodeStringToInteger、RtlIntegerToUnicodeString

0 0
原创粉丝点击