Fix tray messages with different code page

This commit is contained in:
Yukino Song
2024-08-17 01:49:24 +08:00
parent 0826abf607
commit 89677241f8
2 changed files with 53 additions and 4 deletions

32
src/misc.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <string>
#ifdef _WIN32
#include <windows.h>
std::string convertUtf8ToCurrentCodepage(const std::string& utf8Str) {
if (GetACP() == CP_UTF8) {
return std::string(utf8Str);
}
// Step 1: Convert UTF-8 to UTF-16
int utf16Len = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
if (utf16Len == 0) {
throw std::runtime_error("Failed to convert UTF-8 to UTF-16");
}
std::wstring utf16Str(utf16Len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &utf16Str[0], utf16Len);
// Step 2: Convert UTF-16 to the current Windows codepage
int codepageLen = WideCharToMultiByte(GetACP(), 0, utf16Str.c_str(), -1, NULL, 0, NULL, NULL);
if (codepageLen == 0) {
throw std::runtime_error("Failed to convert UTF-16 to current Windows codepage");
}
std::string codepageStr(codepageLen, '\0');
WideCharToMultiByte(GetACP(), 0, utf16Str.c_str(), -1, &codepageStr[0], codepageLen, NULL, NULL);
return codepageStr;
}
#endif