From c6cd2f1f164baac1414f2cf658566de146b10552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 23 Jul 2022 18:59:37 +0200 Subject: Fix display of Unicode text. Turns out that SWI-Prolog's wide string functions, which I started using in 03fe361, do not convert between narrow Prolog atoms and wide C strings, as I mistakenly thought. Instead, they work with wide Prolog atoms. In hindsight, it is not surprising. --- c/common.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'c/common.cpp') diff --git a/c/common.cpp b/c/common.cpp index 4ce95a0..155c099 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -57,6 +57,24 @@ Library::~Library() FreeLibrary(m_hModule); } +/* Convert narrow unmanaged string to narrow/wide basic string. */ +template <> +std::basic_string BstrFromSz(const char* sz, int) +{ + return std::string(sz); +} + +template <> +std::basic_string BstrFromSz(const char* sz, int iCp) +{ + int cbMultiByte = strlen(sz)+1; + int cchWideChar = MultiByteToWideChar(iCp, 0, sz, cbMultiByte, NULL, 0); + std::wstring wstr(cchWideChar, 0); + if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, wstr.data(), cchWideChar)) + throw Win32Error(GetLastError()); + return wstr; +} + /* Move message box to center of main window. */ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM lParam) noexcept { -- cgit v1.2.3