MCPcopy Create free account
hub / github.com/apitrace/apitrace / patchModule

Function patchModule

inject/injectee_mhook.cpp:366–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

364
365
366static void
367patchModule(HMODULE hModule,
368 const char *szModule,
369 Action action)
370{
371 /* Never patch this module */
372 if (hModule == g_hThisModule) {
373 return;
374 }
375
376 /* Never patch our hook module */
377 if (hModule == g_hHookModule) {
378 return;
379 }
380
381 /* Hook modules only once */
382 if (action == ACTION_HOOK) {
383 std::pair< std::set<HMODULE>::iterator, bool > ret;
384 EnterCriticalSection(&g_Mutex);
385 ret = g_hHookedModules.insert(hModule);
386 LeaveCriticalSection(&g_Mutex);
387 if (!ret.second) {
388 return;
389 }
390 }
391
392 if (VERBOSITY > 0) {
393 debugPrintf("inject: found module %s\n", szModule);
394 }
395
396 const char *szBaseName = getBaseName(szModule);
397
398 if (stricmp(szBaseName, "opengl32.dll") != 0 &&
399 stricmp(szBaseName, "dxgi.dll") != 0 &&
400 stricmp(szBaseName, "d3d10.dll") != 0 &&
401 stricmp(szBaseName, "d3d10_1.dll") != 0 &&
402 stricmp(szBaseName, "d3d11.dll") != 0 &&
403 stricmp(szBaseName, "d3d9.dll") != 0 &&
404 stricmp(szBaseName, "d3d8.dll") != 0 &&
405 stricmp(szBaseName, "ddraw.dll") != 0 &&
406 stricmp(szBaseName, "dcomp.dll") != 0 &&
407 stricmp(szBaseName, "dxva2.dll") != 0 &&
408 stricmp(szBaseName, "d2d1.dll") != 0 &&
409 stricmp(szBaseName, "dwrite.dll") != 0) {
410 return;
411 }
412
413 PIMAGE_EXPORT_DIRECTORY pExportDescriptor = getExportDescriptor(g_hHookModule);
414 assert(pExportDescriptor);
415
416 DWORD *pAddressOfNames = (DWORD *)((BYTE *)g_hHookModule + pExportDescriptor->AddressOfNames);
417 for (DWORD i = 0; i < pExportDescriptor->NumberOfNames; ++i) {
418 const char *szFunctionName = (const char *)((BYTE *)g_hHookModule + pAddressOfNames[i]);
419
420 LPVOID lpOrigAddress = (LPVOID)RealGetProcAddress(hModule, szFunctionName);
421 if (lpOrigAddress) {
422 LPVOID lpHookAddress = (LPVOID)RealGetProcAddress(g_hHookModule, szFunctionName);
423 assert(lpHookAddress);

Callers 1

patchAllModulesFunction · 0.70

Calls 4

getBaseNameFunction · 0.85
debugPrintfFunction · 0.70
getExportDescriptorFunction · 0.70
insertMethod · 0.45

Tested by

no test coverage detected