我想对LoadString Win32函数的此包装程序有您的意见。
int LoadWString( HINSTANCE hInstance_In, UINT uID_In, std::wstring &str_Out ){
return LoadStringW( hInstance_In, uID_In, (LPWSTR)str_Out.c_str(), (int)str_Out.max_size());
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,问题更多是关于使用字符串max_size属性作为缓冲区大小,这是否有一些负面的缺点?
c_str()返回一个不可修改的指针。不得写入。抛弃常数并写入受控序列会导致不确定的行为。
相反,只需查询指向资源部分的指针以及字符串长度,然后std::wstring在此数据上构造一个新对象:
std::wstring LoadStringW( unsigned int id )
{
const wchar_t* p = nullptr;
int len = ::LoadStringW( nullptr, id, reinterpret_cast<LPWSTR>( &p ), 0 );
if ( len > 0 )
{
return std::wstring( p, static_cast<size_t>( len ) );
}
// Return empty string; optionally replace with throwing an exception.
return std::wstring();
}
Run Code Online (Sandbox Code Playgroud)
有几点值得注意:
0给nBufferMax。这样做会将指针返回到资源部分;不执行其他内存分配:
nBufferMax:
如果此参数为0,则lpBuffer接收指向资源本身的只读指针。
NUL字符。这要求使用带有显式length参数的std :: string构造函数。return std::wstring(p);可能会截断返回的字符串。| 归档时间: |
|
| 查看次数: |
756 次 |
| 最近记录: |