7 #pragma comment(lib, "DbgHelp.lib")
25 std::map<std::string,FnEntry>
vFn;
29 void HookFunction( CHAR *pSrc, CHAR *pDst, UINT uHint, PVOID pFnNew )
34 memset(&fn,0,
sizeof(fn));
38 fn.
pModule = GetModuleHandleA(pSrc);
40 fn.
pImageDsc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData( fn.
pModule, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize );
44 if( lstrcmpiA(pDst, pName) == 0 )
55 IMAGE_IMPORT_BY_NAME* pImp = (IMAGE_IMPORT_BY_NAME*)((PBYTE)fn.
pModule+fn.
pThunkDat->u1.AddressOfData);
56 if( pImp->Hint == uHint )
72 sprintf_s(Hint,
"%4.4X", uHint);
73 std::string key = std::string(pSrc)+
"|"
74 + std::string(pDst)+
"|"
77 auto fnIt =
vFn.find(key);
78 if( fnIt ==
vFn.end() )
80 vFn.insert( std::make_pair(key, fn) );
87 sprintf_s(Hint,
"%4.4X", uHint);
88 std::string key = std::string(pSrc)+
"|"
89 + std::string(pDst)+
"|"
92 auto fnIt =
vFn.find(key);
93 if( fnIt !=
vFn.end() )
95 WriteFn( fnIt->second.ppFnAdr, fnIt->second.ppFnOld );
104 for(
auto fnIt =
vFn.begin(); fnIt !=
vFn.end(); fnIt++ )
106 WriteFn( fnIt->second.ppFnAdr, fnIt->second.ppFnOld );
113 BOOL bProtectResult = FALSE;
114 DWORD dwOldProtect = 0;
118 MEMORY_BASIC_INFORMATION memInfo;
119 if( VirtualQuery(ppFnAdr, &memInfo,
sizeof( memInfo ) ) > 0 )
121 bProtectResult = VirtualProtect( memInfo.BaseAddress, memInfo.RegionSize, PAGE_READWRITE, &dwOldProtect );
123 ReadProcessMemory( GetCurrentProcess(), ppFnAdr, &ppFnOld,
sizeof(PROC*), &sBytes );
124 WriteProcessMemory( GetCurrentProcess(), ppFnAdr, ppFnNew,
sizeof(PROC*), &sBytes );
127 bProtectResult = VirtualProtect( memInfo.BaseAddress, memInfo.RegionSize, PAGE_READONLY, &dwOldProtect );
PVOID WriteFn(PVOID ppFnAdr, PVOID ppFnNew)
struct FnHook::FnEntry FnEntry
PIMAGE_IMPORT_DESCRIPTOR pImageDsc
std::map< std::string, FnEntry > vFn
PIMAGE_THUNK_DATA pThunkDat
void UnHookFunction(CHAR *pSrc, CHAR *pDst, UINT uHint)
void HookFunction(CHAR *pSrc, CHAR *pDst, UINT uHint, PVOID pFnNew)