ReactOS  0.4.13-dev-551-gf37fb1f
delayimp.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS SDK Library
3  * LICENSE: LGPL, see LGPL.txt in top level directory.
4  * FILE: lib/sdk/delayimp/delayimp.c
5  * PURPOSE: Library for delay importing from dlls
6  * PROGRAMMERS: Timo Kreuzer <timo.kreuzer@reactos.org>
7  * Mark Jansen
8  *
9  */
10 
11 #include <stdarg.h>
12 #include <windef.h>
13 #include <winbase.h>
14 #include <delayimp.h>
15 
16 /**** Linker magic: provide a default (NULL) pointer, but allow the user to override it ****/
17 
18 /* The actual items we use */
21 
22 #if !defined(__GNUC__)
23 /* The fallback symbols */
26 
27 /* Tell the linker to use the fallback symbols */
28 #if defined (_M_IX86)
29 #pragma comment(linker, "/alternatename:___pfnDliNotifyHook2=___pfnDliNotifyHook2Default")
30 #pragma comment(linker, "/alternatename:___pfnDliFailureHook2=___pfnDliFailureHook2Default")
31 #else
32 #pragma comment(linker, "/alternatename:__pfnDliNotifyHook2=__pfnDliNotifyHook2Default")
33 #pragma comment(linker, "/alternatename:__pfnDliFailureHook2=__pfnDliFailureHook2Default")
34 #endif
35 #endif
36 
37 
38 /**** Helper functions to convert from RVA to address ****/
39 
41 unsigned
43 {
44  return pData - pBase;
45 }
46 
47 extern const IMAGE_DOS_HEADER __ImageBase;
48 
50 PVOID
52 {
53  return (PVOID)(((ULONG_PTR)(rva)) + ((ULONG_PTR)&__ImageBase));
54 }
55 
56 
57 /**** load helper ****/
58 
61 {
62  DelayLoadInfo dli = {0};
63  int index;
64  PImgThunkData pIAT;
65  PImgThunkData pINT;
66  HMODULE *phMod;
67 
68  pIAT = PFromRva(pidd->rvaIAT);
69  pINT = PFromRva(pidd->rvaINT);
70  phMod = PFromRva(pidd->rvaHmod);
71  index = IndexFromPImgThunkData(pIATEntry, pIAT);
72 
73  dli.cb = sizeof(dli);
74  dli.pidd = pidd;
75  dli.ppfn = (FARPROC*)&pIAT[index].u1.Function;
76  dli.szDll = PFromRva(pidd->rvaDLLName);
78  if (dli.dlp.fImportByName)
79  {
80  /* u1.AdressOfData points to a IMAGE_IMPORT_BY_NAME struct */
82  dli.dlp.szProcName = (LPCSTR)&piibn->Name;
83  }
84  else
85  {
87  }
88 
90  {
92  if (dli.pfnCur)
93  {
94  pIAT[index].u1.Function = (DWORD_PTR)dli.pfnCur;
97 
98  return dli.pfnCur;
99  }
100  }
101 
102  dli.hmodCur = *phMod;
103 
104  if (dli.hmodCur == NULL)
105  {
108  if (dli.hmodCur == NULL)
109  {
110  dli.hmodCur = LoadLibraryA(dli.szDll);
111  if (dli.hmodCur == NULL)
112  {
113  dli.dwLastError = GetLastError();
116 
117  if (dli.hmodCur == NULL)
118  {
119  ULONG_PTR args[] = { (ULONG_PTR)&dli };
121 
122  /* If we survive the exception, we are expected to use pfnCur directly.. */
123  return dli.pfnCur;
124  }
125  }
126  }
127  *phMod = dli.hmodCur;
128  }
129 
131 
134  if (dli.pfnCur == NULL)
135  {
136  /* dli.dlp.szProcName might also contain the ordinal */
137  dli.pfnCur = GetProcAddress(dli.hmodCur, dli.dlp.szProcName);
138  if (dli.pfnCur == NULL)
139  {
140  dli.dwLastError = GetLastError();
143 
144  if (dli.pfnCur == NULL)
145  {
146  ULONG_PTR args[] = { (ULONG_PTR)&dli };
148  }
149 
150  //return NULL;
151  }
152  }
153 
154  pIAT[index].u1.Function = (DWORD_PTR)dli.pfnCur;
156 
159 
160  return dli.pfnCur;
161 }
162 
FARPROC pfnCur
Definition: delayimp.h:73
#define ERROR_SUCCESS
Definition: deptool.c:10
#define DWORD_PTR
Definition: treelist.c:76
const IMAGE_DOS_HEADER __ImageBase
Definition: mstscax.cpp:17
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL fImportByName
Definition: delayimp.h:57
GLdouble u1
Definition: glext.h:8308
RVA rvaDLLName
Definition: delayimp.h:45
FARPROC(WINAPI * PfnDliHook)(unsigned, PDelayLoadInfo)
Definition: delayimp.h:77
Definition: match.c:390
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define ERROR_SEVERITY_ERROR
Definition: ntbasedef.h:603
LPCSTR szProcName
Definition: delayimp.h:60
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
DelayLoadProc dlp
Definition: delayimp.h:71
DWORD dwLastError
Definition: delayimp.h:74
smooth NULL
Definition: ftsmooth.c:416
HMODULE hmodCur
Definition: delayimp.h:72
#define FORCEINLINE
Definition: ntbasedef.h:221
GLuint index
Definition: glext.h:6031
const char * LPCSTR
Definition: xmlstorage.h:183
PfnDliHook __pfnDliNotifyHook2
Definition: delayimp.c:19
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define WINAPI
Definition: msvc.h:8
PCImgDelayDescr pidd
Definition: delayimp.h:68
#define index(s, c)
Definition: various.h:29
DWORD cb
Definition: delayimp.h:67
LPCSTR szDll
Definition: delayimp.h:70
PfnDliHook __pfnDliNotifyHook2Default
Definition: delayimp.c:24
HANDLE HMODULE
Definition: typedefs.h:75
FARPROC WINAPI __delayLoadHelper2(PCImgDelayDescr pidd, PImgThunkData pIATEntry)
Definition: delayimp.c:60
DWORD dwOrdinal
Definition: delayimp.h:61
#define ERROR_PROC_NOT_FOUND
Definition: winerror.h:199
VOID WINAPI RaiseException(IN DWORD dwExceptionCode, IN DWORD dwExceptionFlags, IN DWORD nNumberOfArguments, IN CONST ULONG_PTR *lpArguments OPTIONAL)
Definition: except.c:693
#define IMAGE_SNAP_BY_ORDINAL(Ordinal)
Definition: ntimage.h:567
union _IMAGE_THUNK_DATA32::@2053 u1
PfnDliHook __pfnDliFailureHook2
Definition: delayimp.c:20
FARPROC * ppfn
Definition: delayimp.h:69
DWORD RVA
Definition: compat.h:903
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:94
#define ULONG_PTR
Definition: config.h:101
#define GetProcAddress(x, y)
Definition: compat.h:410
PfnDliHook __pfnDliFailureHook2Default
Definition: delayimp.c:25
#define VcppException(sev, err)
Definition: delayimp.cpp:29
#define IMAGE_ORDINAL(Ordinal)
Definition: pedump.c:337
FORCEINLINE unsigned IndexFromPImgThunkData(PCImgThunkData pData, PCImgThunkData pBase)
Definition: delayimp.c:42
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
FORCEINLINE PVOID PFromRva(RVA rva)
Definition: delayimp.c:51
int(* FARPROC)()
Definition: compat.h:28