Resolve the running image without passing through the active ANSI code page. * The returned UTF-8 string is heap allocated. Grow dynamically so an * extended-length install path is never mistaken for a complete path. */
| 164 | * The returned UTF-8 string is heap allocated. Grow dynamically so an |
| 165 | * extended-length install path is never mistaken for a complete path. */ |
| 166 | static inline char *cbm_module_path_utf8(void) { |
| 167 | DWORD capacity = 512U; |
| 168 | while (capacity <= 32768U) { |
| 169 | wchar_t *wide = (wchar_t *)calloc((size_t)capacity, sizeof(*wide)); |
| 170 | if (!wide) { |
| 171 | return NULL; |
| 172 | } |
| 173 | SetLastError(ERROR_SUCCESS); |
| 174 | DWORD length = GetModuleFileNameW(NULL, wide, capacity); |
| 175 | if (length > 0U && length < capacity) { |
| 176 | cbm_module_path_unprefix(wide); |
| 177 | char *utf8 = cbm_wide_to_utf8(wide); |
| 178 | free(wide); |
| 179 | return utf8; |
| 180 | } |
| 181 | DWORD error = GetLastError(); |
| 182 | free(wide); |
| 183 | if (length == 0U || (length < capacity && error != ERROR_INSUFFICIENT_BUFFER) || |
| 184 | capacity == 32768U) { |
| 185 | return NULL; |
| 186 | } |
| 187 | capacity *= 2U; |
| 188 | if (capacity > 32768U) { |
| 189 | capacity = 32768U; |
| 190 | } |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | #endif /* _WIN32 */ |
| 196 | #endif /* CBM_WIN_UTF8_H */ |
no test coverage detected