[Interest] Is it safe to use QString::utf16() as a Windows wchar_t*?

Nikos Chantziaras realnc at gmail.com
Fri Nov 10 10:01:38 CET 2017


On 09/11/17 14:56, Giuseppe D'Angelo wrote:
> Il 09/11/2017 10:15, Nikos Chantziaras ha scritto:
>> This saves an allocation, a copy and a free since I don't have to use
>> QString::toWCharArray(). However, is this actually safe? "It seems to
>> work," and AFAIK, QString::d->data() is already in the format and
>> encoding Windows excepts a wchar_t* to be. Is this correct?
> 
> It is safe. QString is UTF-16 encoded (same code unit size as for 
> wchar_t on Windows), and Windows APIs accepting wchar_t expect a 
> NUL-terminated UTF-16 string as a wchar_t*, which is exactly what 
> utf16() provides.
> 
> See also various of such usages into Qt itself, e.g.
> 
>> https://code.woboq.org/qt5/qtbase/src/corelib/io/qfilesystemengine_win.cpp.html#387 

Thanks for the confirmation!




More information about the Interest mailing list