From b8d09eca9a7249f064140baf4fda4c928b5c9472 Mon Sep 17 00:00:00 2001 From: Jerry Evans Date: Sat, 6 Apr 2024 17:23:56 +0100 Subject: [PATCH] Fix convertCharPointerToStdString with MSC_VER guard --- RtAudio.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index bc61c54..b7459be 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -72,9 +72,24 @@ std::string convertCharPointerToStdString(const char *text) } template<> inline -std::string convertCharPointerToStdString(const wchar_t *text) +std::string convertCharPointerToStdString(const wchar_t* text) { +#if defined(_MSC_VER) + if (!text) + return std::string(); + const int wchars = (int)wcslen(text); + // how many characters are required after conversion? + const int nchars = WideCharToMultiByte(CP_UTF8, 0, text, wchars, 0, 0, 0, 0); + if (!nchars) + return std::string(); + // create buffer + std::string nret(nchars, 0); + // do the conversion + WideCharToMultiByte(CP_UTF8, 0, text, wchars, &nret[0], nchars, 0, 0); + return nret; +#else return std::wstring_convert>{}.to_bytes(text); +#endif } #if defined(_MSC_VER)