内核编程的字符串操作

1) ASCII字符串与宽字符串 a) Char:负责ANSI字符集 b) wchar_t负责UNICODE字符集 例如:wchar_t *str2=L”ASCE” c) 打印:wchar*string=L”asce”; kdPrint((“%s/n”,string)); 2) ASCI_STRING字符串与UNICODE_STRING字符串 typedef struct _UNICODE_STRING{ ushort length;//字符的长度,单位是字节。如果是N个字符,则Length为N的2倍 ushort Maximumlength; PWSTR Buffer;//缓冲区的指针 }UNICODE_STRING,*PUNICODE_STRING; 注意:UNICODE_STRING字符串不是以NULL结束 打印:ANSI_STRING ansiString; …… KdPrint((“%Z/n”,&ansiString)); UNICODE_STRING uniString;……KdPrint((“%wZ/n”,&uniString)); 3) 字符的初始化与销毁 a) VOID RtlInitAnsiString(PUNICODE_STRING DestinationString, PCWSTR SourceString); VOID RtlInitUnicodeString(DestinationString, SourceString); 注意:方法简单,用完不要清理内存,但是如果修改SourceString将会导致 DestinationString字符串发生变化 例:ANSI_STRING ansiString; CHAR*string=”asce”; RtlInitAnsiString(&ansiString,string); b) 程序员自己申请内存,并且初始化内存,当不用字符串时,需要回收字符串占用的内存 #define BUFFER_SIZE 1024 UNICODE_STRING UnicodeString={0}; //1)分配内存(按照最大长度分配内存) UnicodeString.MaximumLength=BUFFER_SIZE; UnicodeString.Buffer=(PWSTR)ExAllocatePool(PagedPool,BUFFER_SIZE); WCHAR*wideString=L”ASCE”; //2)设置字符串长度(实际长度),因为是宽字符,所以是字符长度的倍 UnicodeString.Length = 2*wcslen(wideString); //保证缓冲区足够大,否则程序终止 ASSERT(UnicodeString.MaximumLength >= UnicodeString.Length); //3)内存复制 RtlCopyString(UnicodeString.Buffer, wideString, UnicodeString.Length); KdPrint(("UnicodeString: %wZ/n", &UnicodeString)); //4)清理内存 ExFreePool(UnicodeString.Buffer); UnicodeString.Buffer = NULL; UnicodeString.Length = UnicodeString.MaximumLength = 0; 对于最后一步清理内存,DDK给出了简化函数,分别是RtlFreeAnsiString和RtlFreeUnicodeString,这两个函数内部调用了ExFreePool去回收内存的。 4) 字符串复制 VOID RtlCopyString(PSTRING DestinationString,const STRING *SourceString); VOID RtlCopyUnicodeString(PUNICODE_STRING DestinationString,PCUNICODE_STRING SourceString); 5) 字符串比较 LONG RtlCompareString(const STRING *String1, const STRING *String2, BOOLEAN CaseInSensitive) LONG RtlCompareUnicodeString(…….) RtlEqualString RtlEqualUnicodeString 6) 字符串转化成大写 VOID RtlUpperString( PSTRING DestinationString, const STRING *SourceString); NTSTATUS RtlUpcaseUnicodeString( __inout PUNICODE_STRING DestinationString, //目的字符串 __in PCUNICODE_STRING SourceString, //源字符串 __in BOOLEAN AllocateDestinationString //是否为目的字符串分配内存, //目的字符串和源字符串可以是同一个字符串 ); 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 //需要转换的字符串 ); 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 //是否需要对被转换字符串分配内存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值