Use UTF-16 for pipe communication and simplify signaling

Refactored `send_message_to_powerdisplay` to use UTF-16
encoding for pipe communication, aligning with WinUI's
`Encoding.Unicode`. Removed UTF-8 conversion logic and
streamlined payload handling with `CString`.

Simplified the signaling mechanism in `dllmain.cpp` for
notifying `PowerDisplay.exe` of settings updates. The
message now excludes `config` data, relying on
`PowerDisplay.exe` to read the updated `settings.json`
file directly. Updated comments to reflect the new
behavior.
This commit is contained in:
Yu Leng
2025-11-12 17:08:30 +08:00
parent 5d63ca7a9c
commit c24b5d97c5
2 changed files with 15 additions and 21 deletions

View File

@@ -67,24 +67,18 @@ void PowerDisplayProcessManager::send_message_to_powerdisplay(const std::wstring
{
const auto formatted = std::format(L"{}\r\n", message);
// Convert wide string to UTF-8 for C# interop
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, formatted.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (utf8_size > 0)
// Match WinUI side which reads the pipe using UTF-16 (Encoding.Unicode)
const CString payload(formatted.c_str());
const DWORD bytes_to_write = static_cast<DWORD>(payload.GetLength() * sizeof(wchar_t));
DWORD bytes_written = 0;
if (FAILED(m_write_pipe->Write(payload, bytes_to_write, &bytes_written)))
{
std::string utf8_msg(static_cast<size_t>(utf8_size) - 1, '\0'); // -1 to exclude null terminator
WideCharToMultiByte(CP_UTF8, 0, formatted.c_str(), -1, &utf8_msg[0], utf8_size, nullptr, nullptr);
const DWORD bytes_to_write = static_cast<DWORD>(utf8_msg.length());
DWORD bytes_written = 0;
if (FAILED(m_write_pipe->Write(utf8_msg.c_str(), bytes_to_write, &bytes_written)))
{
Logger::error(L"Failed to write message to PowerDisplay pipe");
}
else
{
Logger::trace(L"Sent message to PowerDisplay: {}", message);
}
Logger::error(L"Failed to write message to PowerDisplay pipe");
}
else
{
Logger::trace(L"Sent message to PowerDisplay: {}", message);
}
}
catch (...)

View File

@@ -233,9 +233,9 @@ public:
parse_activation_hotkey(values);
values.save_to_settings_file();
// Notify PowerDisplay.exe that settings have been updated
auto message = std::format(L"{{\"action\":\"settings_updated\",\"config\":{}}}", config);
m_process_manager.send_message_to_powerdisplay(message);
// Notify PowerDisplay.exe that settings have been updated (signal only, no config data)
// PowerDisplay will read the updated settings.json file itself
m_process_manager.send_message_to_powerdisplay(L"{\"action\":\"settings_updated\"}");
}
catch (std::exception&)
{