字符串初始化方式总结

来源:互联网 发布:腾讯程序员待遇 编辑:程序博客网 时间:2024/05/22 09:49

2种字符串初始化方式总结:

//字符串测试函数#define BUFFER_SIZE 1024VOID SetStringTest(){//第一种字符串初始化方式。这种方式的优点是用完后不需要清理内存UNICODE_STRING str;RtlInitUnicodeString(&str,L"hello");//第二种方式是:程序员自己申请内存,然后自己释放UNICODE_STRING unicode_str = {0};//设置缓冲区大小unicode_str.MaximumLength = BUFFER_SIZE;//分配内存unicode_str.Buffer = (PWSTR)ExAllocatePool(PagedPool,BUFFER_SIZE);WCHAR *wideStr = L"Hello";//设置字符长度,我因为是宽字符,所以需要*2unicode_str.Length = 2*wcslen(wideStr);//保证缓冲区足够大,否则终止程序ASSERT(unicode_str.MaximumLength >= unicode_str.Length);//内存复制RtlCopyMemory(unicode_str.Buffer,wideStr,unicode_str.Length);KdPrint(("unicodeStr:%wZ\n",unicode_str));//清理内存ExFreePool(unicode_str.Buffer);unicode_str.Buffer = NULL;unicode_str.Length = unicode_str.MaximumLength = 0;}


0 0
原创粉丝点击