ReactOS 0.4.15-dev-7924-g5949c20
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
41unsigned
43{
44 return pData - pBase;
45}
46
47extern const IMAGE_DOS_HEADER __ImageBase;
48
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.AddressOfData 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 {
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 */
138 if (dli.pfnCur == NULL)
139 {
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
#define index(s, c)
Definition: various.h:29
PfnDliHook __pfnDliFailureHook2
Definition: delayimp.c:20
PfnDliHook __pfnDliNotifyHook2
Definition: delayimp.c:19
const IMAGE_DOS_HEADER __ImageBase
PfnDliHook __pfnDliFailureHook2Default
Definition: delayimp.c:25
FORCEINLINE unsigned IndexFromPImgThunkData(PCImgThunkData pData, PCImgThunkData pBase)
Definition: delayimp.c:42
PfnDliHook __pfnDliNotifyHook2Default
Definition: delayimp.c:24
FARPROC WINAPI __delayLoadHelper2(PCImgDelayDescr pidd, PImgThunkData pIATEntry)
Definition: delayimp.c:60
FORCEINLINE PVOID PFromRva(RVA rva)
Definition: delayimp.c:51
#define VcppException(sev, err)
Definition: delayimp.cpp:32
FARPROC(WINAPI * PfnDliHook)(unsigned, PDelayLoadInfo)
Definition: delayimp.h:77
@ dliNoteEndProcessing
Definition: delayimp.h:39
@ dliFailLoadLib
Definition: delayimp.h:37
@ dliNotePreGetProcAddress
Definition: delayimp.h:36
@ dliNotePreLoadLibrary
Definition: delayimp.h:35
@ dliFailGetProc
Definition: delayimp.h:38
@ dliStartProcessing
Definition: delayimp.h:34
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:104
int(* FARPROC)()
Definition: compat.h:36
DWORD RVA
Definition: compat.h:1262
#define GetProcAddress(x, y)
Definition: compat.h:753
VOID WINAPI RaiseException(_In_ DWORD dwExceptionCode, _In_ DWORD dwExceptionFlags, _In_ DWORD nNumberOfArguments, _In_opt_ const ULONG_PTR *lpArguments)
Definition: except.c:700
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
#define ULONG_PTR
Definition: config.h:101
GLuint index
Definition: glext.h:6031
GLdouble u1
Definition: glext.h:8308
if(dx< 0)
Definition: linetemp.h:194
#define ERROR_SEVERITY_ERROR
Definition: ntbasedef.h:596
#define IMAGE_SNAP_BY_ORDINAL(Ordinal)
Definition: ntimage.h:567
#define IMAGE_ORDINAL(Ordinal)
Definition: pedump.c:337
PCImgDelayDescr pidd
Definition: delayimp.h:68
HMODULE hmodCur
Definition: delayimp.h:72
DWORD cb
Definition: delayimp.h:67
FARPROC pfnCur
Definition: delayimp.h:73
LPCSTR szDll
Definition: delayimp.h:70
DelayLoadProc dlp
Definition: delayimp.h:71
FARPROC * ppfn
Definition: delayimp.h:69
DWORD dwLastError
Definition: delayimp.h:74
BOOL fImportByName
Definition: delayimp.h:57
DWORD dwOrdinal
Definition: delayimp.h:61
LPCSTR szProcName
Definition: delayimp.h:60
RVA rvaDLLName
Definition: delayimp.h:45
union _IMAGE_THUNK_DATA32::@2134 u1
Definition: match.c:390
#define DWORD_PTR
Definition: treelist.c:76
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830
uint32_t ULONG_PTR
Definition: typedefs.h:65
HANDLE HMODULE
Definition: typedefs.h:77
#define FORCEINLINE
Definition: wdftypes.h:67
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
#define ERROR_PROC_NOT_FOUND
Definition: winerror.h:199
const char * LPCSTR
Definition: xmlstorage.h:183