鱼C论坛

 找回密码
 立即注册
查看: 2662|回复: 0

字符串的初始化方式总结

[复制链接]
发表于 2015-4-1 20:50:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有2中字符串的初始化方式。。代码示例如下
  1. //字符串测试函数
  2. #define BUFFER_SIZE 1024
  3. VOID SetStringTest()
  4. {
  5.         //第一种字符串初始化方式。这种方式的优点是用完后不需要清理内存
  6.         UNICODE_STRING str;
  7.         RtlInitUnicodeString(&str,L"hello");

  8.         //第二种方式是:程序员自己申请内存,然后自己释放
  9.         UNICODE_STRING unicode_str = {0};
  10.         //设置缓冲区大小
  11.         unicode_str.MaximumLength = BUFFER_SIZE;
  12.         //分配内存
  13.         unicode_str.Buffer = (PWSTR)ExAllocatePool(PagedPool,BUFFER_SIZE);
  14.         WCHAR *wideStr = L"Hello";

  15.         //设置字符长度,我因为是宽字符,所以需要*2
  16.         unicode_str.Length = 2*wcslen(wideStr);

  17.         //保证缓冲区足够大,否则终止程序
  18.         ASSERT(unicode_str.MaximumLength >= unicode_str.Length);
  19.         //内存复制
  20.         RtlCopyMemory(unicode_str.Buffer,wideStr,unicode_str.Length);
  21.        
  22.         KdPrint(("unicodeStr:%wZ\n",unicode_str));

  23.         //清理内存
  24.         ExFreePool(unicode_str.Buffer);
  25.         unicode_str.Buffer = NULL;
  26.         unicode_str.Length = unicode_str.MaximumLength = 0;
  27. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 15:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表