#pragma once #include #include class StringHelper { public: static LPCWSTR string_to_lpcwstr(const std::string& str) { const int strlen = static_cast(str.length()) + 1; const int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), strlen, nullptr, 0); auto* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), strlen, buf, len); const std::wstring r(buf); delete[] buf; return r.c_str(); } static LPWSTR string_to_lpwstr(const std::string& str) { // Assumes std::string is encoded in the current Windows ANSI codepage const int bufferlen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), nullptr, 0); if (bufferlen == 0) { // Something went wrong. Perhaps, check GetLastError() and log. return nullptr; } // Allocate new LPWSTR - must deallocate it later LPWSTR widestr = new WCHAR[bufferlen + 1]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), widestr, bufferlen); // Ensure wide string is null terminated widestr[bufferlen] = 0; // Do something with widestr return widestr; //delete[] widestr; } };