fix: Windows native code bugs and dedup
- fix unsigned underflow in Utf8FromUtf16 when WideCharToMultiByte fails - fix inverted freopen_s logic in console attach - remove misleading WS_EX_APPWINDOW from GWL_STYLE mask - close mutex handle on normal exit - extract SetHDRStateForTarget, EnableComposition/DisableComposition, DisplayModeToMap helpers - cache GetWindowsVersion result
This commit is contained in:
@@ -10,12 +10,8 @@
|
||||
void CreateAndAttachConsole() {
|
||||
if (::AllocConsole()) {
|
||||
FILE *unused;
|
||||
if (freopen_s(&unused, "CONOUT$", "w", stdout)) {
|
||||
_dup2(_fileno(stdout), 1);
|
||||
}
|
||||
if (freopen_s(&unused, "CONOUT$", "w", stderr)) {
|
||||
_dup2(_fileno(stdout), 2);
|
||||
}
|
||||
freopen_s(&unused, "CONOUT$", "w", stdout);
|
||||
freopen_s(&unused, "CONOUT$", "w", stderr);
|
||||
std::ios::sync_with_stdio();
|
||||
FlutterDesktopResyncOutputStreams();
|
||||
}
|
||||
@@ -45,15 +41,15 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
|
||||
if (utf16_string == nullptr) {
|
||||
return std::string();
|
||||
}
|
||||
unsigned int target_length = ::WideCharToMultiByte(
|
||||
int raw_length = ::WideCharToMultiByte(
|
||||
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
|
||||
-1, nullptr, 0, nullptr, nullptr)
|
||||
-1; // remove the trailing null character
|
||||
-1, nullptr, 0, nullptr, nullptr);
|
||||
if (raw_length <= 1) {
|
||||
return std::string();
|
||||
}
|
||||
unsigned int target_length = raw_length - 1; // exclude trailing null
|
||||
int input_length = (int)wcslen(utf16_string);
|
||||
std::string utf8_string;
|
||||
if (target_length == 0 || target_length > utf8_string.max_size()) {
|
||||
return utf8_string;
|
||||
}
|
||||
utf8_string.resize(target_length);
|
||||
int converted_length = ::WideCharToMultiByte(
|
||||
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
|
||||
|
||||
Reference in New Issue
Block a user