内核中字符串释疑

    技术2022-05-19  21

    转自:http://hi.baidu.com/tianyamingyuedao/blog/item/8d04dd16263c660c972b43eb.html

     

    1.字符串结构

    驱动中字符串有UNIOCDE_STRING和ANSI_STRING两种结构

    UNICODE_STRING定义如下:

    typedef struct _UNICODE_STRING{

    USHORT Length;           //字符串长度(字节计算),无符号短整型,双字节

    USHORT MaxinumLength;    //字符串缓冲区长度(字节计算)

    PWSTR    Buffer;           //字符串缓冲区指针(双字节表示一个字符)

    }UNICODE_STRING,*PUNICODE_STRING

    ANSI_STRING定义如下:

    typedef struct _STRING{

    USHORT Length;           //字符串长度(字节计算),无符号短整型,双字节

    USHORT MaxinumLength;    //字符串缓冲区长度(字节计算)

    PSTR    Buffer;            //字符串缓冲区指针(单字节表示一个字符)

    }ANSI_STRING,*PANSI_STRING

    驱动中主要都是UNICODE编码字符串,ANSI少数场合使用。

    2.字符串操作RTL系列函数

    ★字符串的初始化RtlInitUnicodeString

    UNICODE_STRING str;

    RtlInitUniocdeString(&str,L”unicodestring”);

    ★字符串的拷贝 RtlCopyUnicodeString

    1.不分配内存字符串拷贝

    UNICODE_STRING dst;       //目标字符串

    WCHAR dst_buf[256];       //缓冲区

    UNICODE_STRING src;       //源字符串

    RtlInitUniocdeString(&src,L”unicodestring”);

    RrlInitEmptyUnicodeString(&dst,dst_buf,256*sizeof(WCHAAR)); //把目标字符串初始化为缓冲区长度为256双字的UNICODE_STRING结构

    RtlCopyUnicodeString(&dst,&src);    //字符串拷贝

    2.动态分配内存拷贝

    #define MEM_TAG ‘My’    //定义内存分配标记

    UNICODE_STRING dst={0};     

    UNICODE_STRING src={0};        

    RtlInitUnicodeString(&src,L”unicodestring”);

    dst.Buffer=(PWCHAR)ExAllocatePoolWithTag(NonpagePool,src.Length,MEM_TAG); //给目标字符串动态分配大小

    if(dst.Buffer==NULL)

    {

        错误处理

    }

    dst.Length=dst.MaximumLength=src.Length;

    RtlCopyUnicodeString(&dst,&src);

    //内存泄漏问题,用完要释放内存

    ExFreePool(dst.Buffer);

    dst.Buffer==NULL;

    dst.Length=dst.MaximumLength=0;

    ★字符串的连接 RtlAppendUnicodeStringToString

    UNICODE_STRING dst;

    dst.Buffer=(PWCHAR)ExAllocatePoolWithTag(NonPagedPool,256,MEM_TAG);

    UNICODE_STRING src;

    RtlInitUnicodeString(&src,L”myunicode”);

    RtlCopyUnicodeString(&dst,&src);

    RtlInitUnicodeString(&src,L”string”);

    RtlAppendUnicodeStringToString(&dst,&src);

    KdPrint((“%wZ”,&dst));

    ExFreePool(dst.Buffer);

    WCHAR buf[256];

    UNICODE_STRING dst,src;

    RtlInitEmptyUnicodeString(&dst,buf,256*sizeof(WCHAR));

    RtlInitUnicodeString(&src,L"myunicode");

    RtlCopyUnicodeString(&dst,&src);

    RtlInitUnicodeString(&src,L"string");

    RtlAppendUnicodeStringToString(&dst,&src);

    KdPrint(("%wZ",&dst));

    3.DbgPrint格式说明符

    4.DbgPrint函数打印字符串

    UNICODE_STRING src;

    DbgPrint(“%wZ”,&src);

    DbgPrint(“%S”,src.Buffer);

    DbgPrint(“%ws”,src.Buffer);

    不推荐使用后2个

    ANSI_STRING src;

    DbgPrint(“%Z”,&src);

    DbgPrint(“%s”,src.Buffer);

    其他格式跟printf一样

    5.ANSI_STRING和UNICODE_STRING转换

    RtlUnicodeStringToAnsiString()

    RtlUnicodeStringToAnsiString()

    UNICODE_STRING stc;  ANSI_STRING   dst;  RtlInitAnsiString(&dst,”Hello World!”);  RtlAnsiStringToUnicodeString(&src,&dst,TRUE);//需要释放内存。 RtlFreeUnicodeString(&src);//释放动态分配的内存。

    6.汉字的内核打印输出

    由于DbgPrint函数遇到中文字符的UNICODE_STRING就会截断,转换成

    ANSI_STRING打印输出。

    UNICODE_STRING src;

    ANSI_STRING dst;

    RtlInitUnicodeString(&src,L”打印汉字”);

    RtlUnicodeStringToAnsiString(&dst,&src,TRUE);

    DbgPrint(“%Z”,&dst);

    RtlFreeAnsiString(&dst);


    最新回复(0)