There is nothing wrong with returning LPCWSTR from a function. A common problem that arises is managing the memory around the return value. A LPCWSTR usually contained in allocated memory, so ownership of this memory must be understood between the caller and the callee.
I do not want to select your sample, but this is a good example of what might go wrong with the return of LPCWSTR . The memory for the string is stored by the wss instance, which is local to this function. As soon as the function returns the memory, it is freed in the wss destructor and, therefore, the return value is invalid.
If you are already using C ++, my recommendation would be to simply return std::string or wstring to eliminate confusion about who owns the allocated memory.
wstring int2LPCWSTR ( int integer ) { wstringstream wss; wss << integer; return wss.str(); }
Or if copying the value around is a problem, return it by reference.
void int2LPCWSTR ( int integer, wstring& value ) { wstringstream wss; wss << integer; value = wss.str(); }
source share