f53353a944d3c9659e1fe0790f330c338439bfbd
[reactos.git] / reactos / sdk / lib / delayimp / delayimp.c
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 *
8 */
9
10 #include <stdarg.h>
11 #include <windef.h>
12 #include <winbase.h>
13 #include <delayimp.h>
14
15 /**** load helper ****/
16
17 FARPROC WINAPI
18 __delayLoadHelper2(PCImgDelayDescr pidd, PImgThunkData pIATEntry)
19 {
20 DelayLoadInfo dli;
21 int index;
22 PImgThunkData pIAT;
23 PImgThunkData pINT;
24 HMODULE *phMod;
25 FARPROC pProc;
26
27 pIAT = PFromRva(pidd->rvaIAT);
28 pINT = PFromRva(pidd->rvaINT);
29 phMod = PFromRva(pidd->rvaHmod);
30 index = IndexFromPImgThunkData(pIATEntry, pIAT);
31
32 dli.cb = sizeof(dli);
33 dli.pidd = pidd;
34 dli.ppfn = (FARPROC*)pIATEntry->u1.Function;
35 dli.szDll = PFromRva(pidd->rvaDLLName);
36 dli.dlp.fImportByName = !(pINT[index].u1.Ordinal & IMAGE_ORDINAL_FLAG);
37 if (dli.dlp.fImportByName)
38 {
39 /* u1.AdressOfData points to a IMAGE_IMPORT_BY_NAME struct */
40 PIMAGE_IMPORT_BY_NAME piibn = PFromRva((RVA)pINT[index].u1.AddressOfData);
41 dli.dlp.szProcName = (LPCSTR)&piibn->Name;
42 }
43 else
44 {
45 dli.dlp.dwOrdinal = pINT[index].u1.Ordinal & ~IMAGE_ORDINAL_FLAG;
46 }
47 dli.hmodCur = *phMod;
48 dli.pfnCur = (FARPROC)pIAT[index].u1.Function;
49 dli.dwLastError = GetLastError();
50 pProc = __pfnDliNotifyHook2(dliStartProcessing, &dli);
51 if (pProc)
52 {
53 pIAT[index].u1.Function = (DWORD_PTR)pProc;
54 return pProc;
55 }
56
57 if (dli.hmodCur == NULL)
58 {
59 dli.hmodCur = LoadLibraryA(dli.szDll);
60 if (!dli.hmodCur)
61 {
62 dli.dwLastError = GetLastError();
63 __pfnDliFailureHook2(dliFailLoadLib, &dli);
64 // if (ret)
65 // {
66 // }
67 // FIXME: raise exception;
68 return NULL;
69 }
70 *phMod = dli.hmodCur;
71 }
72
73 /* dli.dlp.szProcName might also contain the ordinal */
74 pProc = GetProcAddress(dli.hmodCur, dli.dlp.szProcName);
75 if (!pProc)
76 {
77 dli.dwLastError = GetLastError();
78 __pfnDliFailureHook2(dliFailGetProc, &dli);
79 // FIXME: handle return value & raise exception
80 return NULL;
81 }
82 pIAT[index].u1.Function = (DWORD_PTR)pProc;
83
84 return pProc;
85 }
86
87 /*** The default hooks ***/
88
89 FARPROC WINAPI
90 DefaultDliNotifyHook2(unsigned dliNotify, PDelayLoadInfo pdli)
91 {
92 return NULL;
93 }
94
95 FARPROC WINAPI
96 DefaultDliFailureHook2(unsigned dliNotify, PDelayLoadInfo pdli)
97 {
98 return NULL;
99 }
100
101 PfnDliHook __pfnDliNotifyHook2 = DefaultDliNotifyHook2;
102 PfnDliHook __pfnDliFailureHook2 = DefaultDliFailureHook2;