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. */
| 221 | * The returned UTF-8 string is heap allocated. Grow dynamically so an |
| 222 | * extended-length install path is never mistaken for a complete path. */ |
| 223 | static inline char *cbm_module_path_utf8(void) { |
| 224 | DWORD capacity = 512U; |
| 225 | while (capacity <= 32768U) { |
| 226 | wchar_t *wide = (wchar_t *)calloc((size_t)capacity, sizeof(*wide)); |
| 227 | if (!wide) { |
| 228 | return NULL; |
| 229 | } |
| 230 | SetLastError(ERROR_SUCCESS); |
| 231 | DWORD length = GetModuleFileNameW(NULL, wide, capacity); |
| 232 | if (length > 0U && length < capacity) { |
| 233 | cbm_module_path_unprefix(wide); |
| 234 | char *utf8 = cbm_wide_to_utf8(wide); |
| 235 | free(wide); |
| 236 | return utf8; |
| 237 | } |
| 238 | DWORD error = GetLastError(); |
| 239 | free(wide); |
| 240 | if (length == 0U || (length < capacity && error != ERROR_INSUFFICIENT_BUFFER) || |
| 241 | capacity == 32768U) { |
| 242 | return NULL; |
| 243 | } |
| 244 | capacity *= 2U; |
| 245 | if (capacity > 32768U) { |
| 246 | capacity = 32768U; |
| 247 | } |
| 248 | } |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | #endif /* _WIN32 */ |
| 253 | #endif /* CBM_WIN_UTF8_H */ |