Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Dependencies/Utility/Utility/wchar_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,27 @@ typedef WCHAR* LPWSTR;
// MultiByteToWideChar
#define CP_ACP 0
#define MultiByteToWideChar(cp, flags, mbstr, cb, wcstr, cch) mbstowcs(wcstr, mbstr, cch)
#define WideCharToMultiByte(cp, flags, wcstr, cch, mbstr, cb, defchar, used) wcstombs(mbstr, wcstr, cb)

// WideCharToMultiByte replacement:
// The real Windows API supports a "query mode" where mbstr==nullptr and cb==0, which returns
// the required buffer size. wcstombs(nullptr, wcstr, 0) is unreliable on some CRT
// implementations (e.g. Windows CRT via MinGW), so we use an inline function instead.
inline int WideCharToMultiByte(unsigned int /*cp*/, unsigned long /*flags*/,
const wchar_t* wcstr, int /*cch*/,
char* mbstr, int cb,
const char* /*defchar*/, int* /*used*/)
{
if (!wcstr)
return 0;
if (!mbstr || cb == 0)
{
// Query mode: return the required buffer size (number of bytes including null terminator)
size_t len = wcslen(wcstr);
return static_cast<int>(len + 1);
}
size_t converted = wcstombs(mbstr, wcstr, static_cast<size_t>(cb));
if (converted == static_cast<size_t>(-1))
return 0;
return static_cast<int>(converted);
}

Loading