| 677 | |
| 678 | template< class T > |
| 679 | void |
| 680 | patchDescriptor(HMODULE hModule, |
| 681 | const char *szModule, |
| 682 | T pImportDescriptor, |
| 683 | Action action) |
| 684 | { |
| 685 | const char* szDescriptorName = getDescriptorName(hModule, pImportDescriptor); |
| 686 | |
| 687 | ModulesMap::const_iterator modIt = modulesMap.find(szDescriptorName); |
| 688 | if (modIt != modulesMap.end()) { |
| 689 | const char *szMatchModule = modIt->first; // same as szDescriptorName |
| 690 | const Module & module = modIt->second; |
| 691 | |
| 692 | const FunctionMap & functionMap = module.functionMap; |
| 693 | FunctionMap::const_iterator fnIt; |
| 694 | for (fnIt = functionMap.begin(); fnIt != functionMap.end(); ++fnIt) { |
| 695 | const char *szFunctionName = fnIt->first; |
| 696 | LPVOID lpHookAddress = fnIt->second; |
| 697 | |
| 698 | // Knowning the real address is useful when patching imports by ordinal |
| 699 | LPVOID lpRealAddress = NULL; |
| 700 | HMODULE hRealModule = GetModuleHandleA(szDescriptorName); |
| 701 | if (hRealModule) { |
| 702 | // FIXME: this assertion can fail when the wrapper name is the same as the original DLL |
| 703 | //assert(hRealModule != g_hHookModule); |
| 704 | if (hRealModule != g_hHookModule) { |
| 705 | lpRealAddress = (LPVOID)GetProcAddress(hRealModule, szFunctionName); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | LPVOID lpOldAddress = lpRealAddress; |
| 710 | LPVOID lpNewAddress = lpHookAddress; |
| 711 | |
| 712 | if (action == ACTION_UNHOOK) { |
| 713 | std::swap(lpOldAddress, lpNewAddress); |
| 714 | } |
| 715 | |
| 716 | BOOL bPatched; |
| 717 | bPatched = patchFunction(hModule, szModule, szMatchModule, pImportDescriptor, szFunctionName, lpOldAddress, lpNewAddress); |
| 718 | if (action == ACTION_HOOK && bPatched && !module.bInternal && pSharedMem) { |
| 719 | pSharedMem->bReplaced = TRUE; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | |
| 726 | static void |
no test coverage detected