From 91f6aae9ef7b2877828d6e293eb0659b980bea09 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 28 Jun 2024 11:12:17 +0200 Subject: [PATCH] impr: Make Win32 API properly use unicode support --- cmake/build_helpers.cmake | 1 + lib/libimhex/source/api/task_manager.cpp | 2 +- lib/libimhex/source/helpers/utils.cpp | 2 +- main/gui/source/messaging/win.cpp | 8 ++++---- main/gui/source/window/win_window.cpp | 6 +++--- .../source/content/providers/disk_provider.cpp | 12 ++++++------ plugins/windows/include/views/view_tty_console.hpp | 4 ++-- plugins/windows/source/plugin_windows.cpp | 4 ++-- plugins/windows/source/views/view_tty_console.cpp | 12 ++++++------ 9 files changed, 26 insertions(+), 25 deletions(-) diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index 6d62da015..af57af946 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -59,6 +59,7 @@ macro(detectOS) set(CMAKE_INSTALL_LIBDIR ".") set(PLUGINS_INSTALL_LOCATION "plugins") add_compile_definitions(WIN32_LEAN_AND_MEAN) + add_compile_definitions(UNICODE) elseif (APPLE) add_compile_definitions(OS_MACOS) set(CMAKE_INSTALL_BINDIR ".") diff --git a/lib/libimhex/source/api/task_manager.cpp b/lib/libimhex/source/api/task_manager.cpp index fbe9a3bf9..25b4e10bb 100644 --- a/lib/libimhex/source/api/task_manager.cpp +++ b/lib/libimhex/source/api/task_manager.cpp @@ -443,7 +443,7 @@ namespace hex { static auto setThreadDescription = reinterpret_cast( reinterpret_cast( ::GetProcAddress( - ::GetModuleHandle("Kernel32.dll"), + ::GetModuleHandleW(L"Kernel32.dll"), "SetThreadDescription" ) ) diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 1ac7d2ff7..1bc33bdb4 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -355,7 +355,7 @@ namespace hex { url = "https://" + url; #if defined(OS_WINDOWS) - ShellExecute(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + ShellExecuteA(nullptr, "open", url.c_str(), nullptr, nullptr, SW_SHOWNORMAL); #elif defined(OS_MACOS) openWebpageMacos(url.c_str()); #elif defined(OS_LINUX) diff --git a/main/gui/source/messaging/win.cpp b/main/gui/source/messaging/win.cpp index 684f04a12..1e0d7ea71 100644 --- a/main/gui/source/messaging/win.cpp +++ b/main/gui/source/messaging/win.cpp @@ -16,7 +16,7 @@ namespace hex::messaging { // Get the window name auto length = ::GetWindowTextLength(hWnd); std::string windowName(length + 1, '\x00'); - ::GetWindowText(hWnd, windowName.data(), windowName.size()); + ::GetWindowTextA(hWnd, windowName.data(), windowName.size()); // Check if the window is visible and if it's an ImHex window if (::IsWindowVisible(hWnd) == TRUE && length != 0) { @@ -66,13 +66,13 @@ namespace hex::messaging { bool setupNative() { - constexpr static auto UniqueMutexId = "ImHex/a477ea68-e334-4d07-a439-4f159c683763"; + constexpr static auto UniqueMutexId = L"ImHex/a477ea68-e334-4d07-a439-4f159c683763"; // Check if an ImHex instance is already running by opening a global mutex - HANDLE globalMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, UniqueMutexId); + HANDLE globalMutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, UniqueMutexId); if (globalMutex == nullptr) { // If no ImHex instance is running, create a new global mutex - globalMutex = CreateMutex(nullptr, FALSE, UniqueMutexId); + globalMutex = CreateMutexW(nullptr, FALSE, UniqueMutexId); return true; } else { return false; diff --git a/main/gui/source/window/win_window.cpp b/main/gui/source/window/win_window.cpp index 4c7687f62..8d63da250 100644 --- a/main/gui/source/window/win_window.cpp +++ b/main/gui/source/window/win_window.cpp @@ -38,7 +38,7 @@ namespace hex { void nativeErrorMessage(const std::string &message) { log::fatal(message); - MessageBox(nullptr, message.c_str(), "Error", MB_ICONERROR | MB_OK); + MessageBoxA(nullptr, message.c_str(), "Error", MB_ICONERROR | MB_OK); } // Custom Window procedure for receiving OS events @@ -78,7 +78,7 @@ namespace hex { // Handle Windows theme changes if (lParam == 0) break; - if (LPCTSTR(lParam) == std::string_view("ImmersiveColorSet")) { + if (reinterpret_cast(lParam) == std::wstring_view(L"ImmersiveColorSet")) { EventOSThemeChanged::post(); } @@ -86,7 +86,7 @@ namespace hex { } case WM_SETCURSOR: { if (LOWORD(lParam) != HTCLIENT) { - return CallWindowProc((WNDPROC)s_oldWndProc, hwnd, uMsg, wParam, lParam); + return CallWindowProc(reinterpret_cast(s_oldWndProc), hwnd, uMsg, wParam, lParam); } else { switch (ImGui::GetMouseCursor()) { case ImGuiMouseCursor_Arrow: diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index b9409808a..374d15576 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -381,8 +381,8 @@ namespace hex::plugin::builtin { m_availableDrives.clear(); - std::array deviceInstanceID = {}; - std::array description = {}; + std::array deviceInstanceId = {}; + std::array description = {}; const GUID hddClass = GUID_DEVINTERFACE_DISK; @@ -404,7 +404,7 @@ namespace hex::plugin::builtin { if (!SetupDiEnumInterfaceDevice(hDevInfo, nullptr, &hddClass, i, &interfaceData)) break; - if (CM_Get_Device_ID(deviceInfoData.DevInst, deviceInstanceID.data(), MAX_PATH, 0) != CR_SUCCESS) + if (CM_Get_Device_IDW(deviceInfoData.DevInst, deviceInstanceId.data(), MAX_PATH, 0) != CR_SUCCESS) continue; // Get the required size of the device path @@ -419,19 +419,19 @@ namespace hex::plugin::builtin { if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, data, requiredSize, nullptr, nullptr)) continue; - auto path = data->DevicePath; + auto path = reinterpret_cast(data->DevicePath); // Query the friendly name of the device DWORD size = 0; DWORD propertyRegDataType = SPDRP_PHYSICAL_DEVICE_OBJECT_NAME; - SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, + SetupDiGetDeviceRegistryPropertyW(hDevInfo, &deviceInfoData, SPDRP_FRIENDLYNAME, &propertyRegDataType, reinterpret_cast(description.data()), sizeof(description), &size); auto friendlyName = description.data(); - m_availableDrives.insert({ path, friendlyName }); + m_availableDrives.insert({ utf16ToUtf8(path), utf16ToUtf8(friendlyName) }); } // Add all logical drives diff --git a/plugins/windows/include/views/view_tty_console.hpp b/plugins/windows/include/views/view_tty_console.hpp index ace6a99a4..010a36a05 100644 --- a/plugins/windows/include/views/view_tty_console.hpp +++ b/plugins/windows/include/views/view_tty_console.hpp @@ -17,9 +17,9 @@ namespace hex::plugin::windows { void drawContent() override; private: - std::vector> m_comPorts; + std::vector> m_comPorts; - std::vector> getAvailablePorts() const; + std::vector> getAvailablePorts() const; bool connect(); bool disconnect(); diff --git a/plugins/windows/source/plugin_windows.cpp b/plugins/windows/source/plugin_windows.cpp index b50e76fbd..a1ffc3ff2 100644 --- a/plugins/windows/source/plugin_windows.cpp +++ b/plugins/windows/source/plugin_windows.cpp @@ -28,11 +28,11 @@ static void detectSystemTheme() { return; HKEY hkey; - if (RegOpenKey(HKEY_CURRENT_USER, R"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)", &hkey) == ERROR_SUCCESS) { + if (RegOpenKeyW(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)", &hkey) == ERROR_SUCCESS) { DWORD value = 0; DWORD size = sizeof(DWORD); - auto error = RegQueryValueEx(hkey, "AppsUseLightTheme", nullptr, nullptr, reinterpret_cast(&value), &size); + auto error = RegQueryValueExW(hkey, L"AppsUseLightTheme", nullptr, nullptr, reinterpret_cast(&value), &size); if (error == ERROR_SUCCESS) { RequestChangeTheme::post(value == 0 ? "Dark" : "Light"); } diff --git a/plugins/windows/source/views/view_tty_console.cpp b/plugins/windows/source/views/view_tty_console.cpp index f14d0fd5d..c8f56f8fe 100644 --- a/plugins/windows/source/views/view_tty_console.cpp +++ b/plugins/windows/source/views/view_tty_console.cpp @@ -169,14 +169,14 @@ namespace hex::plugin::windows { } } - std::vector> ViewTTYConsole::getAvailablePorts() const { - std::vector> result; - std::vector buffer(0xFFF, 0x00); + std::vector> ViewTTYConsole::getAvailablePorts() const { + std::vector> result; + std::vector buffer(0xFFF, 0x00); for (u16 portNumber = 0; portNumber <= 255; portNumber++) { - std::string port = "COM" + std::to_string(portNumber); + std::wstring port = L"COM" + std::to_wstring(portNumber); - if (::QueryDosDevice(port.c_str(), buffer.data(), buffer.size()) != 0) { + if (::QueryDosDeviceW(port.c_str(), buffer.data(), buffer.size()) != 0) { result.emplace_back(port, buffer.data()); } } @@ -189,7 +189,7 @@ namespace hex::plugin::windows { ui::ToastError::open("hex.windows.view.tty_console.no_available_port"_lang); return true; // If false, connect_error error popup will override this error popup } - m_portHandle = ::CreateFile((R"(\\.\)" + m_comPorts[m_selectedPort].first).c_str(), + m_portHandle = ::CreateFileW((LR"(\\.\)" + m_comPorts[m_selectedPort].first).c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr,