ReactOS 0.4.16-dev-122-g325d74c
apitest_iathook.h
Go to the documentation of this file.
1#ifndef _APITEST_IATHOOK_H
2#define _APITEST_IATHOOK_H
3
5{
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
20static 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
57static 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
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:1279
static BOOL RestoreIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction)
static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName)
static BOOL RedirectIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR *OriginalFunction)
#define skip(...)
Definition: atltest.h:64
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define RtlImageDirectoryEntryToData
Definition: compat.h:809
int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
Definition: locale.c:4223
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define PCHAR
Definition: match.c:90
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define IMAGE_SNAP_BY_ORDINAL32(Ordinal)
Definition: ntimage.h:524
PIMAGE_THUNK_DATA32 PIMAGE_THUNK_DATA
Definition: ntimage.h:566
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
BYTE * PBYTE
Definition: pedump.c:66
struct _IMAGE_IMPORT_BY_NAME * PIMAGE_IMPORT_BY_NAME
union _IMAGE_THUNK_DATA32::@2138 u1
const char * PCSTR
Definition: typedefs.h:52
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
BOOL NTAPI VirtualProtect(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
Definition: virtmem.c:135