According to some older StackOverflow questions ( Unable to pass std::wstring across DLL , C++ DLL returning pointer to std::list<std::wstring> ) it's not considered safe for a C++ DLL to return a std::wstring
because there's no guarantee the main program has the same definition of std::wstring
and therefore it might cause a crash.
However, in http://en.cppreference.com/w/cpp/string/basic_string , it seems std::wstring
can be used interchangeably with a WCHAR
array now:
(Since C++11) The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can be passed to functions that expect a pointer to the first element of a CharT[] array.
I've tested this by passing &s[0]
to a WINAPI function that expected a WCHAR*
buffer and it appeared to work (the std::wstring
was correctly populated with the results of the WINAPI). So since std::wstring
can apparently be treated like a WCHAR
array now, I decided to revisit this question: can a std::wstring
be safely returned from a DLL? Why or why not?
std::wstring
can be safely passed to a WinAPI but not to a program built with a different C++ compiler. Wouldn't the direct modification of thewstring
's character array invalidate whatever other information thewstring
implementation has? But for some reason that worked, so it seemed intuitive to me thatwstring
had gotten more receptive to other forms of modification/assignment. – Veta