#ifndef IAT_HOOK_H
#define IAT_HOOK_H

#include <Windows.h>

// The IAT hook routines must handle x86 and x86-64 differently: 
#if _WIN64
	#define HOOK64
#else
	#define HOOK32
#endif

#ifdef HOOK64

// TODO: 64-bit hooking code
//QWORD HookImportedFunction(HMODULE hMod, char* pszDllName, char* pszFunctionName, QWORD qwNewAddress);

#else

///////////////////////////////////////////////////////////////////////////////
// This code should locate the import section from the process's header, and 
// find the IMAGE_IMPORT_DESCRIPTOR of the DLL that exports the function. 
// We search for the Import Address Table entry first by the name of the DLL.
// We then locate the IMAGE_THUNK_DATA, which holds the original address of 
// the imported function, and replace the function address with the user-
// supplied one.
//
// Parameters: hMODULE hMod = handle to the executable whose header you
//                            want to hook. Almost always the parent EXE?
//             char* pszDllName = name of the module that exported the API
//             char* pszFunctionName = name of the imported API to be hooked
//             DWORD dwNewAddres = 32-bit address of your hook procedure
//
// Returns: DWORD representing the replaced address. Call this in your
//          hook procedure, before or after you make the desired changes
//          to the original function's behavior.
//
// Example usage: 
// originalFunctionAddress = HookImportedFunction(GetModuleHandle(NULL),
//                      "kernel32.dll","LoadLibraryA",(DWORD)MyHookFunction);
//
// Note: GetModuleHandle(NULL) returns a handle to the parent process.
//
// Note: To properly ensure all instances of a call to this imported function
//       are hooked, you ought to first hook GetProcAddress(), for code that
//       does not use an IAT lookup to get an imported function address.
//
DWORD HookImportedFunction(HMODULE hMod, char* pszDllName, char* pszFunctionName, DWORD dwNewAddress);

#endif

#endif