diff --git a/gmodule/gmodule-win32.c b/gmodule/gmodule-win32.c index fef4d893d..2aea8bae9 100644 --- a/gmodule/gmodule-win32.c +++ b/gmodule/gmodule-win32.c @@ -131,7 +131,20 @@ find_in_any_module_using_toolhelp (const gchar *symbol_name) /* Under UWP, Module32Next and Module32First are not available since we're * not allowed to search in the address space of arbitrary loaded DLLs */ #if !defined(G_WINAPI_ONLY_APP) - if ((snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, 0)) == (HANDLE) -1) + /* https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot#remarks + * If the function fails with ERROR_BAD_LENGTH, retry the function until it succeeds. */ + while (TRUE) + { + snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, 0); + if (snapshot == INVALID_HANDLE_VALUE && GetLastError () == ERROR_BAD_LENGTH) + { + g_thread_yield (); + continue; + } + break; + } + + if (snapshot == INVALID_HANDLE_VALUE) return NULL; me32.dwSize = sizeof (me32);