comparison src/shared-fcns.h @ 29537:93a751f468d9

Windows: Allow non-ASCII characters in Octave home directory. * src/shared-fcns.h (w32_get_octave_home): Use Unicode Windows API to get Octave home directory.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 17 Apr 2021 09:16:41 +0200
parents 0a5b15007766
children aef11bb4e6d1
comparison
equal deleted inserted replaced
29536:205033e01792 29537:93a751f468d9
32 32
33 #include <cctype> 33 #include <cctype>
34 34
35 #if defined (OCTAVE_USE_WINDOWS_API) 35 #if defined (OCTAVE_USE_WINDOWS_API)
36 36
37 #include <windows.h> 37 # include <windows.h>
38 #include <tlhelp32.h> 38 # include <tlhelp32.h>
39 39 # include <locale>
40 #if defined (_MSC_VER) 40 # include <codecvt>
41 # define popen _popen 41
42 # define pclose _pclose 42 # if defined (_MSC_VER)
43 #endif 43 # define popen _popen
44 # define pclose _pclose
45 # endif
44 46
45 static std::string 47 static std::string
46 w32_get_octave_home (void) 48 w32_get_octave_home (void)
47 { 49 {
48 std::string retval; 50 std::string retval;
49 51
50 std::string bin_dir; 52 std::string bin_dir;
51 53
52 char namebuf[MAX_PATH+1]; 54 wchar_t namebuf[MAX_PATH+1];
53 if (GetModuleFileName (GetModuleHandle (nullptr), namebuf, MAX_PATH)) 55 DWORD n_size
56 = GetModuleFileNameW (GetModuleHandle (nullptr), namebuf, MAX_PATH);
57 if (n_size < MAX_PATH)
54 { 58 {
55 namebuf[MAX_PATH] = '\0'; 59 // convert wide character string to multibyte UTF-8 string
56 60 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> wchar_conv;
57 std::string exe_name = namebuf; 61 std::string exe_name
62 = wchar_conv.to_bytes (std::wstring (namebuf, n_size));
63
58 size_t pos = exe_name.rfind ('\\'); 64 size_t pos = exe_name.rfind ('\\');
59 65
60 if (pos != std::string::npos) 66 if (pos != std::string::npos)
61 bin_dir = exe_name.substr (0, pos + 1); 67 bin_dir = exe_name.substr (0, pos + 1);
62 } 68 }