ReactOS  0.4.14-dev-342-gdc047f9
apitest_iathook.h
Go to the documentation of this file.
1 #ifndef _APITEST_IATHOOK_H
2 #define _APITEST_IATHOOK_H
3 
5 {
6  ULONG Size;
8  while (ImportDescriptor->Name && ImportDescriptor->OriginalFirstThunk)
9  {
10  PCHAR Name = (PCHAR)(DllBase + ImportDescriptor->Name);
11  if (!lstrcmpiA(Name, DllName))
12  {
13  return ImportDescriptor;
14  }
15  ImportDescriptor++;
16  }
17  return NULL;
18 }
19 
20 static BOOL RedirectIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR* OriginalFunction)
21 {
22  PBYTE DllBase = (PBYTE)TargetDll;
23  PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = FindImportDescriptor(DllBase, DllName);
24  if (ImportDescriptor)
25  {
26  // On loaded images, OriginalFirstThunk points to the name / ordinal of the function
27  PIMAGE_THUNK_DATA OriginalThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->OriginalFirstThunk);
28  // FirstThunk points to the resolved address.
29  PIMAGE_THUNK_DATA FirstThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->FirstThunk);
30  while (OriginalThunk->u1.AddressOfData && FirstThunk->u1.Function)
31  {
32  if (!IMAGE_SNAP_BY_ORDINAL32(OriginalThunk->u1.AddressOfData))
33  {
34  PIMAGE_IMPORT_BY_NAME ImportName = (PIMAGE_IMPORT_BY_NAME)(DllBase + OriginalThunk->u1.AddressOfData);
35  if (!lstrcmpiA((PCSTR)ImportName->Name, FunctionName))
36  {
37  DWORD dwOld;
38  VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), PAGE_EXECUTE_READWRITE, &dwOld);
39  *OriginalFunction = FirstThunk->u1.Function;
40  FirstThunk->u1.Function = NewFunction;
41  VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), dwOld, &dwOld);
42  return TRUE;
43  }
44  }
45  OriginalThunk++;
46  FirstThunk++;
47  }
48  skip("Unable to find the Import %s!%s\n", DllName, FunctionName);
49  }
50  else
51  {
52  skip("Unable to find the ImportDescriptor for %s\n", DllName);
53  }
54  return FALSE;
55 }
56 
57 static BOOL RestoreIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction)
58 {
59  ULONG_PTR old = 0;
60  return RedirectIat(TargetDll, DllName, FunctionName, OriginalFunction, &old);
61 }
62 
63  #endif // _APITEST_IATHOOK_H
64 
signed char * PCHAR
Definition: retypes.h:7
#define TRUE
Definition: types.h:120
int WINAPI lstrcmpiA(LPCSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:42
struct _IMAGE_IMPORT_BY_NAME * PIMAGE_IMPORT_BY_NAME
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define IMAGE_SNAP_BY_ORDINAL32(Ordinal)
Definition: ntimage.h:524
unsigned int BOOL
Definition: ntddk_ex.h:94
static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName)
smooth NULL
Definition: ftsmooth.c:416
static BOOL RestoreIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction)
#define PCHAR
Definition: match.c:90
union _IMAGE_THUNK_DATA32::@2073 u1
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL NTAPI VirtualProtect(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
Definition: virtmem.c:144
#define RtlImageDirectoryEntryToData
Definition: compat.h:468
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
static BOOL RedirectIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR *OriginalFunction)
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char * FunctionName
Definition: acpixf.h:1270
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
#define skip(...)
Definition: atltest.h:64
unsigned int ULONG
Definition: retypes.h:1
const char * PCSTR
Definition: typedefs.h:51
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
BYTE * PBYTE
Definition: pedump.c:66
PIMAGE_THUNK_DATA32 PIMAGE_THUNK_DATA
Definition: ntimage.h:566