From 0b687c3482890255f6a4494a0d74b817057cf439 Mon Sep 17 00:00:00 2001 From: Jerry Evans Date: Sat, 6 Apr 2024 15:47:00 +0100 Subject: [PATCH] Fix Windows deprecation warnings for std::codecvt using WideCharToMultiByte --- RtAudio.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/RtAudio.cpp b/RtAudio.cpp index bc61c545..4708d7bc 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -74,7 +74,22 @@ std::string convertCharPointerToStdString(const char *text) template<> inline std::string convertCharPointerToStdString(const wchar_t *text) { +#if 1 + 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)