Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95251
  • 博文数量: 46
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 505
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 19:56
文章分类
文章存档

2008年(46)

我的朋友

分类: WINDOWS

2008-06-22 15:32:13

字符串:

驱动开发下使用_UNICODE_STRING(主要是用这个)或者是_STRING表示字符串。

typedef struct _UNICODE_STRING {

        USHORT Length;              // 字符串的长度(字节数)

        USHORT MaximumLength;       // 字符串缓冲区的长度(字节数)

        PWSTR   Buffer;             // 字符串缓冲区

    } UNICODE_STRING, *PUNICODE_STRING;

其实根本没有给该字符串分配空间存储字符串的内容。

字符串的初始化:

UNICODE_STRING str;

    str.Buffer = L”my first string!”;

    str.Length = str.MaximumLength = wcslen(L”my first string!”) * sizeof(WCHAR);

   或者是

UNICODE_STRING str = {

        sizeof(L”my first string!”) – sizeof((L”my first string!”)[0]),

        sizeof(L”my first string!”),

        L”my first_string!” };

 

但是这样比较麻烦,可以使用中的宏:

UNICODE_STRING str = RTL_CONSTANT_STRING(L“my first string!”);

或者是

   UNICODE_STRING str;

    RtlInitUnicodeString(&str,L”my first string!”);

   或者是

   INOCODE_STRING dst;

   wchar dst_buf[256];

   RtlInitEmptyString(dst,dst_buf,256*sizeof(WCHAR));

   或者是

    UNICODE_STRING dst=RTL_CONST_STRING(L“this is a const string”);

 

字符串的拷贝:

    使用宏RtlCopyUnicodeString

     UNICODE_STRING dst;         

     WCHAR dst_buf[256];             

     UNICODE_STRING src = RTL_CONST_STRING(L”My source string!”);

     RtlInitEmptyString(dst,dst_buf,256*sizeof(WCHAR));

     RtlCopyUnicodeString(&dst,&src);  

 字符串的连接:

   使用宏 RtlAppendUnicodeToString

 

    NTSTATUS status;

    UNICODE_STRING dst;       

    WCHAR dst_buf[256];            

    UNICODE_STRING src = RTL_CONST_STRING(L”My source string!”);

    RtlInitEmptyString(dst,dst_buf,256*sizeof(WCHAR));

    RtlCopyUnicodeString(&dst,&src);   

   

    status = RtlAppendUnicodeToString(

            &dst,L”my second string!”);//就算dst空间不够也会让内存动 

扩充其空间,此时会返回给status为STATUS_BUFFER_TOO_SMALL

    if(status != STATUS_SUCCESS)

    {

        ……

    }

字符串的输出:

可以使用sprintf或者是swprintf,但是考虑到字符串没有以\0结束,所以建议使用RtlStringCbPrintfW

 

    WCHAR buf[512] = { 0 };

    UNICODE_STRING dst;

    NTSTATUS status;

    ……

   RtlInitEmptyString(dst,dst_buf,512*sizeof(WCHAR));

   status = RtlStringCbPrintfW(

   dst->Buffer,L”file path = %wZ file size = %d \r\n”

        &file_path,file_size);

    // 这里调用wcslen没问题,这是因为RtlStringCbPrintfW打印的

    // 字符串是以空结束的。

    dst->Length = wcslen(dst->Buffer) * sizeof(WCHAR);

 

阅读(1868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~