// Apply hooks to all the modules in this process that we haven't hooked yet. void HookAllLoadedModules() { static HMODULE hooked[MAX_MODULES]; // sorted list of all hooked modules static int nhooked = 0; // Get a list of all the modules in the current process. HANDLE process = GetCurrentProcess(); HMODULE modules[MAX_MODULES]; DWORD length = 0; if (!EnumProcessModules(process, modules, sizeof(modules), &length)) return; int nmodules = (int) (length / sizeof(HMODULE)); // Include the main executable of the process. modules[nmodules++] = GetModuleHandle(NULL); // Find out what module we're running in right now so we can avoid hooking it. HMODULE thismodule = NULL; GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (char*) HookAllLoadedModules, &thismodule); // Check each module against our list of previously hooked modules. for (int i = 0; i < nmodules; i++) { if (modules[i] != thismodule && !bsearch(&(modules[i]), hooked, nhooked, sizeof(HMODULE), ComparePointers)) { // We've found a new one. Apply hooks and add it to our list. HookModule(modules[i]); hooked[nhooked++] = modules[i]; qsort(hooked, nhooked, sizeof(HMODULE), ComparePointers); } } }
HRESULT HookDxDiag(REFIID riid, LPVOID FAR* ppv) { HMODULE dxdlib; OutTraceDW("CoCreateInstance: CLSID_DxDiagProvider object\n"); dxdlib=(*pLoadLibraryA)("dxdiagn.dll"); OutTraceDW("CoCreateInstance: dxdiagn lib handle=%x\n", dxdlib); extern void HookModule(HMODULE, int); HookModule(dxdlib, 0); switch (*(DWORD *)&riid){ case 0x9C6B4CB0: OutTraceDW("CoCreateInstance: IID_DxDiagProvider RIID\n"); // IID_DxDiagProvider::QueryInterface SetHook((void *)(**(DWORD **)ppv), extQueryInterfaceDD, (void **)&pQueryInterfaceDD, "QueryInterface(DxDiag)"); // IID_DxDiagProvider::Initialize SetHook((void *)(**(DWORD **)ppv + 12), extInitializeDD, (void **)&pInitializeDD, "Initialize(DxDiag)"); // IID_DxDiagProvider::Initialize SetHook((void *)(**(DWORD **)ppv + 16), extGetRootContainer, (void **)&pGetRootContainer, "GetRootContainer(DxDiag)"); break; case 0x7D0F462F: OutTraceDW("CoCreateInstance: IID_IDxDiagContainer RIID\n"); break; } return DD_OK; }
PatchIat(Process const& process, std::wstring const& module, std::string const& function, DetourFuncT const& detour, ContextT context = ContextT()) : process_{&process}, module_(detail::ToUpperOrdinal(module)), function_(function), detour_{detour}, context_(std::move(context)) { hadesmem::ModuleList const modules{process}; for (auto const& m : modules) { HookModule(m); } }
extern "C" void* dlopen(const char* filename, int flag) { if (!ORIG_dlopen) ORIG_dlopen = reinterpret_cast<_dlopen>(dlsym(RTLD_NEXT, "dlopen")); auto rv = ORIG_dlopen(filename, flag); if (DebugEnabled()) EngineDevMsg("Engine call: dlopen( \"%s\", %d ) => %p\n", filename, flag, rv); if (rv) { std::lock_guard<std::mutex> lock(dl_refcount_mutex); ++dl_refcount[rv]; } if (rv && filename) HookModule(Convert(filename)); return rv; }