GetModuleFilename failed with: Error 126 "The specified module could not be found" on EnumProcessModules results

1.2k Views Asked by At

I generated a list of modules from the EnumProcessModules() result and the try to get filenames of modules of that list via GetModuleFilename() function.

auto sizeInBytes = getModulesSize();
std::vector<HMODULE> hmodules(sizeInBytes / sizeof(HMODULE), 0);
if (!EnumProcessModules(d_process, hmodules.data(), sizeInBytes, &sizeInBytes))
{
  CUSTOM_THROW("failed to load process modules);
}

WCHAR buffer[MAX_PATH];
std::vector<std::wstring> result;
for (auto hmod : hmodules)
{
  DWORD size = GetModuleFileName(hmod, buffer, MAX_PATH);
  if (!size)
  {
    CUSTOM_THROW("failed to get module path");
  }
}

But sporadically, with unknown reason, sometimes the exception "failed to get module path" is thrown. How can I investigate which modules cause that exception in runtime or else?

Another question is what can cause that module is loaded and available via EnumProcessModules, but become unavailable via GetModuleFileName?

0

There are 0 best solutions below