comparison src/helper.cpp @ 16:51229bd6c24b

Correct use of multibyte/widechar conversion routines on Windows
author Chris Cannam
date Fri, 28 Oct 2016 12:10:54 +0100
parents 92e72014d979
children 78baaad4a001
comparison
equal deleted inserted replaced
15:45ad9f91f977 16:51229bd6c24b
61 #include <process.h> 61 #include <process.h>
62 #include <string> 62 #include <string>
63 #ifdef UNICODE 63 #ifdef UNICODE
64 static HMODULE LoadLibraryUTF8(std::string name) { 64 static HMODULE LoadLibraryUTF8(std::string name) {
65 int n = name.size(); 65 int n = name.size();
66 wchar_t *wname = new wchar_t[n*2+1]; 66 int wn = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), n, 0, 0);
67 MultiByteToWideChar(CP_UTF8, 0, name.c_str(), n, wname, n*2); 67 wchar_t *wname = new wchar_t[wn+1];
68 wn = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), n, wname, wn);
69 wname[wn] = L'\0';
68 HMODULE h = LoadLibraryW(wname); 70 HMODULE h = LoadLibraryW(wname);
69 delete[] wname; 71 delete[] wname;
70 return h; 72 return h;
71 } 73 }
72 static std::string GetErrorText() { 74 static std::string GetErrorText() {
79 NULL, 81 NULL,
80 err, 82 err,
81 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 83 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
82 (LPTSTR) &buffer, 84 (LPTSTR) &buffer,
83 0, NULL ); 85 0, NULL );
84 int n = wcslen(buffer); 86 int wn = wcslen(buffer);
85 char *text = new char[n*8 + 1]; 87 int n = WideCharToMultiByte(CP_UTF8, 0, buffer, wn, 0, 0, 0, 0);
86 WideCharToMultiByte(CP_UTF8, 0, buffer, n, text, n*8, 0, 0); 88 char *text = new char[n+1];
89 n = WideCharToMultiByte(CP_UTF8, 0, buffer, wn, text, n, 0, 0);
90 text[n] = '\0';
87 std::string s(text); 91 std::string s(text);
88 LocalFree(&buffer); 92 LocalFree(&buffer);
89 delete[] text; 93 delete[] text;
90 for (int i = s.size(); i > 0; ) { 94 for (int i = s.size(); i > 0; ) {
91 --i; 95 --i;