- UID
- 2
- 精华
- 积分
- 7736
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
- VOID RtlInitUnicodeString (OUT PUNICODE_STRING DestinationString,IN PCWSTR SourceString OPTIONAL)
- {
- SIZE_T Length;
- DestinationString->MaximumLength = 0;
- DestinationString->Length = 0;
- DestinationString->Buffer = (PWSTR)SourceString;
- if (ARGUMENT_PRESENT(SourceString))
- {
- Length = wcslen(SourceString) * sizeof(WCHAR);
- ASSERT(Length < MAX_USTRING);
- if(Length >= MAX_USTRING)
- {
- Length = MAX_USTRING - sizeof(UNICODE_NULL);
- }
- DestinationString->Length = (USHORT)Length;
- DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
- }
- return;
- }
- NTSTATUS RtlInitUnicodeStringEx ( OUT PUNICODE_STRING DestinationString,IN PCWSTR SourceString OPTIONAL)
- {
- SIZE_T Length;
- DestinationString->Length = 0;
- DestinationString->MaximumLength = 0;
- DestinationString->Buffer = (PWSTR)SourceString;
- if (ARGUMENT_PRESENT(SourceString))
- {
- Length = wcslen(SourceString);
- // We are actually limited to 32765 characters since we want to store a meaningful MaximumLength also.
- if (Length > (UNICODE_STRING_MAX_CHARS - 1))
- {
- return STATUS_NAME_TOO_LONG;
- }
- Length *= sizeof(WCHAR);
- DestinationString->Length = (USHORT)Length;
- DestinationString->MaximumLength = (USHORT)(Length + sizeof(WCHAR));
- }
- return STATUS_SUCCESS;
- }
复制代码
从以上代码可见,这2个函数会将传入的字符串指针直接赋值给结构体,这样的话,如果传入的是栈字符串,那么UNICODE_STIRNG只能在当前域内使用,不能存储到其他生命周期更长的地方,否则栈恢复以后读取到不正确的数据,
然而传入一个全局字符串是可以的,例如:
UNICODE_STRING str1;
void func()
{
WCHAR buf[]=L"lich";
RtlInitUnicodeString(&str1,buf);
}
这是错误写法 |
|