ReactOS  0.4.14-dev-838-g99f979d
delayimp.cpp File Reference
#include <apitest.h>
#include <strsafe.h>
#include <delayimp.h>
#include <mmsystem.h>
#include <winver.h>
#include <shlwapi.h>
#include <intshcut.h>
#include <sfc.h>
#include <imagehlp.h>
#include <mmddk.h>
Include dependency graph for delayimp.cpp:

Go to the source code of this file.

Classes

struct  UnProtect
 

Macros

#define FACILITY_VISUALCPP   ((LONG)0x6d)
 
#define VcppException(sev, err)   ((sev) | (FACILITY_VISUALCPP<<16) | err)
 
#define WINMM_DLLNAME   "WINMM.dll"
 
#define LAST_DLI   333
 
#define CheckDli   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : CheckDli_imp
 
#define CheckDliDone   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : CheckDliDone_imp
 

Functions

chartarget (PDelayLoadInfo pdli)
 
static void SetExpectedDli (unsigned *order)
 
static void CheckDli_imp (unsigned dliNotify, PDelayLoadInfo pdli, BOOL ErrorHandler)
 
static void CheckDliDone_imp ()
 
INT_PTR WINAPI MyFunction ()
 
BOOL WINAPI MySfcIsKeyProtected (HKEY KeyHandle, LPCWSTR SubKeyName, REGSAM KeySam)
 
FARPROC WINAPI DliHook (unsigned dliNotify, PDelayLoadInfo pdli)
 
FARPROC WINAPI DliFailHook (unsigned dliNotify, PDelayLoadInfo pdli)
 
LONG ExceptionFilter (IN PEXCEPTION_POINTERS ExceptionInfo, ULONG ExceptionCode)
 
template<typename PTR >
PTR Rva2Addr (PIMAGE_DOS_HEADER dos, RVA rva)
 
 START_TEST (delayimp)
 

Variables

bool g_BreakFunctionName = false
 
bool g_BrokenFunctionName = false
 
bool g_BypassMost = false
 
bool g_ExceptionIsModule = false
 
bool g_ImportByName = true
 
const charg_ExpectedDll = NULL
 
const charg_ExpectedName = NULL
 
char g_Target [100] = { 0 }
 
unsignedg_DliHookExpected = NULL
 
size_t g_DliHookIndex = 0
 
static HMODULE g_VersionDll
 
PfnDliHook __pfnDliNotifyHook2 = DliHook
 
bool g_UsePointers = false
 
unsigned g_winmm_get_cur_task [] = { dliStartProcessing, dliNotePreLoadLibrary, dliNotePreGetProcAddress, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_winmm_midi_out_close [] = { dliStartProcessing, dliNotePreGetProcAddress, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_winmm_mide_in_close [] = { dliStartProcessing, dliNotePreGetProcAddress, dliFailGetProc, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_sfc_key [] = { dliStartProcessing, dliNotePreLoadLibrary, dliFailLoadLib, dliNotePreGetProcAddress, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_sfc_file [] = { dliStartProcessing, dliNotePreGetProcAddress, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_version_a [] = { dliStartProcessing, dliNotePreLoadLibrary, dliNotePreGetProcAddress, dliFailGetProc, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_version_w [] = { dliStartProcessing, dliNotePreGetProcAddress, dliFailGetProc, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_scard [] = { dliStartProcessing, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_shlwapi [] = { dliStartProcessing, dliNotePreLoadLibrary, dliNotePreGetProcAddress, dliNoteEndProcessing, LAST_DLI }
 
unsigned g_imagehlp [] = { dliStartProcessing, dliNotePreLoadLibrary, dliFailLoadLib, LAST_DLI }
 

Macro Definition Documentation

◆ CheckDli

#define CheckDli   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : CheckDli_imp

Definition at line 126 of file delayimp.cpp.

◆ CheckDliDone

#define CheckDliDone   (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : CheckDliDone_imp

Definition at line 127 of file delayimp.cpp.

◆ FACILITY_VISUALCPP

#define FACILITY_VISUALCPP   ((LONG)0x6d)

Definition at line 26 of file delayimp.cpp.

◆ LAST_DLI

#define LAST_DLI   333

Definition at line 86 of file delayimp.cpp.

◆ VcppException

#define VcppException (   sev,
  err 
)    ((sev) | (FACILITY_VISUALCPP<<16) | err)

Definition at line 30 of file delayimp.cpp.

◆ WINMM_DLLNAME

#define WINMM_DLLNAME   "WINMM.dll"

Definition at line 36 of file delayimp.cpp.

Function Documentation

◆ CheckDli_imp()

static void CheckDli_imp ( unsigned  dliNotify,
PDelayLoadInfo  pdli,
BOOL  ErrorHandler 
)
static

Definition at line 95 of file delayimp.cpp.

96 {
97  if (!g_DliHookExpected) return;
98 
99  winetest_ok(dliNotify == g_DliHookExpected[g_DliHookIndex], "Expected dliNotify to be %u, was: %u for %s\n",
100  g_DliHookExpected[g_DliHookIndex], dliNotify, target(pdli));
101  if (ErrorHandler)
102  {
103  winetest_ok(dliNotify == dliFailGetProc || dliNotify == dliFailLoadLib,
104  "Expected code %u to be processed by the Hook, not the ErrorHandler for %s\n", dliNotify, target(pdli));
105  }
106  else
107  {
108  winetest_ok(dliNotify == dliStartProcessing || dliNotify == dliNotePreLoadLibrary ||
109  dliNotify == dliNotePreGetProcAddress || dliNotify == dliNoteEndProcessing,
110  "Expected code %u to be processed by the ErrorHandler, not the Hook for %s\n", dliNotify, target(pdli));
111  }
113  g_DliHookIndex++;
114 }
size_t g_DliHookIndex
Definition: delayimp.cpp:85
void __winetest_cdecl winetest_ok(int condition, const char *msg,...)
#define LAST_DLI
Definition: delayimp.cpp:86
unsigned * g_DliHookExpected
Definition: delayimp.cpp:84
char * target(PDelayLoadInfo pdli)
Definition: delayimp.cpp:48

◆ CheckDliDone_imp()

static void CheckDliDone_imp ( )
static

Definition at line 116 of file delayimp.cpp.

117 {
118  if (!g_DliHookExpected) return;
120  "Expected g_DliHookExpected[g_DliHookIndex] to be %u, was: %u for %s\n",
123  g_Target[0] = '\0';
124 }
char g_Target[100]
Definition: delayimp.cpp:46
size_t g_DliHookIndex
Definition: delayimp.cpp:85
smooth NULL
Definition: ftsmooth.c:416
void __winetest_cdecl winetest_ok(int condition, const char *msg,...)
#define LAST_DLI
Definition: delayimp.cpp:86
unsigned * g_DliHookExpected
Definition: delayimp.cpp:84
char * target(PDelayLoadInfo pdli)
Definition: delayimp.cpp:48

◆ DliFailHook()

FARPROC WINAPI DliFailHook ( unsigned  dliNotify,
PDelayLoadInfo  pdli 
)

Definition at line 256 of file delayimp.cpp.

257 {
258  ok(pdli && pdli->cb >= 36,
259  "Expected a valid pointer with a struct that is big enough: %p, %lu\n", pdli, pdli ? pdli->cb : 0u);
260  if (!pdli || pdli->cb < 36) return NULL;
261 
262  CheckDli(dliNotify, pdli, TRUE);
263 
264  /* Redirections / fixes */
265  if (dliNotify == dliFailLoadLib)
266  {
267  if (!_stricmp(pdli->szDll, "l_m_os.dll"))
268  return (FARPROC)LoadLibraryA("sfc_os.dll");
269  }
270  else if (dliNotify == dliFailGetProc)
271  {
272  if (pdli->dlp.fImportByName && pdli->hmodCur == (HMODULE)1)
273  {
274  return GetProcAddress(g_VersionDll, pdli->dlp.szProcName);
275  }
276  }
277 
278  /* Parameter validation */
279  ok(pdli->ppfn != NULL, "Expected ppfn to be valid, was NULL for %s\n", target(pdli));
280  ok(pdli->szDll != NULL, "Expected szDll to be valid, was NULL for %s\n", target(pdli));
281  if (pdli->dlp.fImportByName)
282  ok(pdli->dlp.szProcName != NULL, "Expected szProcName to be valid, was NULL for %s\n", target(pdli));
283  else
284  ok(pdli->dlp.dwOrdinal != 0, "Expected dwOrdinal to be valid, was NULL for %s\n", target(pdli));
285  switch(dliNotify)
286  {
287  case dliFailLoadLib:
288  ok(pdli->hmodCur == NULL, "Expected hmodCur to be NULL, was: %p for %s\n", pdli->hmodCur, target(pdli));
289  ok(pdli->pfnCur == NULL, "Expected pfnCur to be NULL, was: %p for %s\n", pdli->pfnCur, target(pdli));
291  "Expected dwLastError to be ERROR_MOD_NOT_FOUND, was: %lu for %s\n", pdli->dwLastError, target(pdli));
292  break;
293  case dliFailGetProc:
294  ok(pdli->hmodCur != NULL, "Expected hmodCur to be valid, was NULL for %s\n", target(pdli));
295  ok(pdli->pfnCur == NULL, "Expected pfnCur to be NULL, was: %p for %s\n", pdli->pfnCur, target(pdli));
297  "Expected dwLastError to be ERROR_PROC_NOT_FOUND, was: %lu for %s\n", pdli->dwLastError, target(pdli));
298  break;
299  }
300 
301  return NULL;
302 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
FARPROC pfnCur
Definition: delayimp.h:73
#define TRUE
Definition: types.h:120
BOOL fImportByName
Definition: delayimp.h:57
#define _stricmp
Definition: cat.c:22
#define CheckDli
Definition: delayimp.cpp:126
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
DWORD cb
Definition: delayimp.h:67
LPCSTR szDll
Definition: delayimp.h:70
DWORD dwOrdinal
Definition: delayimp.h:61
#define ERROR_PROC_NOT_FOUND
Definition: winerror.h:199
#define ok(value,...)
Definition: atltest.h:57
FARPROC * ppfn
Definition: delayimp.h:69
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:94
GLenum target
Definition: glext.h:7315
#define GetProcAddress(x, y)
Definition: compat.h:418
static HMODULE g_VersionDll
Definition: delayimp.cpp:142
int(* FARPROC)()
Definition: compat.h:28

Referenced by START_TEST().

◆ DliHook()

FARPROC WINAPI DliHook ( unsigned  dliNotify,
PDelayLoadInfo  pdli 
)

Definition at line 143 of file delayimp.cpp.

144 {
145  ok(pdli && pdli->cb >= 36, "Expected a valid pointer with a struct that is big enough: %p, %lu\n",
146  pdli, pdli ? pdli->cb : 0u);
147  if (!pdli || pdli->cb < 36) return NULL;
148 
149  CheckDli(dliNotify, pdli, FALSE);
150 
152  {
153  g_BreakFunctionName = false;
154  g_BrokenFunctionName = true;
155  char* procname = (char*)pdli->dlp.szProcName;
156  UnProtect prot(procname);
157  char c = procname[0];
158  procname[0] = isupper(c) ? tolower(c) : toupper(c);
159  }
160 
161  /* Validate dll name when required */
163  {
164  ok(!strcmp(g_ExpectedDll, pdli->szDll), "Expected szDll to be '%s', but was: '%s'\n", g_ExpectedDll, pdli->szDll);
165  ok(pdli->dlp.fImportByName, "Expected import by name (%s!%s)\n", g_ExpectedDll, g_ExpectedName);
166  if (pdli->dlp.fImportByName)
167  {
168  ok(!strcmp(g_ExpectedName, pdli->dlp.szProcName), "Expected szProcName to be '%s', but was: '%s'\n",
169  g_ExpectedName, pdli->dlp.szProcName);
170  }
171  }
172 
173 
174  if (dliNotify == dliStartProcessing)
175  {
176  /* Test loadlib fail */
177  if (!_stricmp(pdli->szDll, "sfc_os.dll"))
178  {
179  char* dll = (char*)pdli->szDll;
180  UnProtect u(dll);
181  dll[0] = 'l'; dll[1] = '_'; dll[2] = 'm';
182  }
183  if (!_stricmp(pdli->szDll, "imagehlp.dll"))
184  {
185  char* dll = (char*)pdli->szDll;
186  UnProtect u(dll);
187  dll[0] = 'x'; dll[1] = 'x'; dll[2] = 'x'; dll[3] = 'x'; dll[4] = 'x';
188  }
189  /* Test bypass */
190  if (!_stricmp(pdli->szDll, "dbghelp.dll"))
191  return MyFunction;
192  }
193  else if (dliNotify == dliNotePreLoadLibrary)
194  {
195  /* Show that this value is actually used! */
196  if (!_stricmp(pdli->szDll, "version.dll"))
197  {
198  g_VersionDll = LoadLibraryA("version.dll");
199  return (FARPROC)1;
200  }
201 
202  }
203  else if (dliNotify == dliNotePreGetProcAddress)
204  {
205  if (pdli->dlp.fImportByName && !strcmp(pdli->dlp.szProcName, "SfcIsKeyProtected"))
206  {
208  }
209  }
210 
211  /* Parameter validation */
212  ok(pdli->ppfn != NULL, "Expected ppfn to be valid, was NULL for %s\n", target(pdli));
213  ok(pdli->szDll != NULL, "Expected szDll to be valid, was NULL for %s\n", target(pdli));
214  ok(pdli->dwLastError == ERROR_SUCCESS,
215  "Expected dwLastError to be ERROR_SUCCESS, was: %lu for %s\n", pdli->dwLastError, target(pdli));
216  ok(g_ImportByName == !!pdli->dlp.fImportByName, "Expected pdli->dlp.fImportByName to equal g_ImportByname\n");
217  if (pdli->dlp.fImportByName)
218  ok(pdli->dlp.szProcName != NULL, "Expected szProcName to be valid, was NULL for %s\n", target(pdli));
219  else
220  ok(pdli->dlp.dwOrdinal != 0, "Expected dwOrdinal to be valid, was NULL for %s\n", target(pdli));
221  switch(dliNotify)
222  {
223  case dliStartProcessing:
224  ok(pdli->hmodCur == NULL, "Expected hmodCur to be NULL, was: %p for %s\n", pdli->hmodCur, target(pdli));
225  ok(pdli->pfnCur == NULL, "Expected pfnCur to be NULL, was: %p for %s\n", pdli->pfnCur, target(pdli));
226  break;
228  ok(pdli->hmodCur == NULL, "Expected hmodCur to be NULL, was: %p for %s\n", pdli->hmodCur, target(pdli));
229  ok(pdli->pfnCur == NULL, "Expected pfnCur to be NULL, was: %p for %s\n", pdli->pfnCur, target(pdli));
230  break;
232  ok(pdli->hmodCur != NULL, "Expected hmodCur to be valid, was NULL for %s\n", target(pdli));
233  ok(pdli->pfnCur == NULL, "Expected pfnCur to be NULL, was: %p for %s\n", pdli->pfnCur, target(pdli));
234  break;
236  if (!g_BypassMost)
237  ok(pdli->hmodCur != NULL, "Expected hmodCur to be valid, was NULL for %s\n", target(pdli));
238  ok(pdli->pfnCur != NULL, "Expected pfnCur to be a valid pointer, was NULL for %s\n", target(pdli));
240  {
242  ok(targetProc != NULL, "This should not happen, the function i need is unavail! (%s!%s)\n",
244  ok(targetProc == pdli->pfnCur, "Expected pfnCur to be %p, was %p for %s\n", targetProc, pdli->pfnCur, target(pdli));
245  ok(pdli->ppfn && targetProc == *pdli->ppfn,
246  "Expected ppfn to be valid and to result in %p, was: %p(%p) for %s\n",
247  target, pdli->ppfn, pdli->ppfn ? *pdli->ppfn : NULL, target(pdli));
248  }
249  break;
250  default:
251  break;
252  }
253  return NULL;
254 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
FARPROC pfnCur
Definition: delayimp.h:73
BOOL WINAPI MySfcIsKeyProtected(HKEY KeyHandle, LPCWSTR SubKeyName, REGSAM KeySam)
Definition: delayimp.cpp:136
#define ERROR_SUCCESS
Definition: deptool.c:10
bool g_BypassMost
Definition: delayimp.cpp:41
BOOL fImportByName
Definition: delayimp.h:57
#define _stricmp
Definition: cat.c:22
static HMODULE dll
Definition: str.c:188
#define CheckDli
Definition: delayimp.cpp:126
bool g_ImportByName
Definition: delayimp.cpp:43
INT_PTR WINAPI MyFunction()
Definition: delayimp.cpp:131
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
int toupper(int c)
Definition: utclib.c:881
#define isupper(c)
Definition: acclib.h:71
bool g_BrokenFunctionName
Definition: delayimp.cpp:40
const GLubyte * c
Definition: glext.h:8905
DWORD cb
Definition: delayimp.h:67
LPCSTR szDll
Definition: delayimp.h:70
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:821
DWORD dwOrdinal
Definition: delayimp.h:61
#define ok(value,...)
Definition: atltest.h:57
bool g_BreakFunctionName
Definition: delayimp.cpp:39
FARPROC * ppfn
Definition: delayimp.h:69
const char * g_ExpectedName
Definition: delayimp.cpp:45
GLenum target
Definition: glext.h:7315
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define GetProcAddress(x, y)
Definition: compat.h:418
const char * g_ExpectedDll
Definition: delayimp.cpp:44
static HMODULE g_VersionDll
Definition: delayimp.cpp:142
int tolower(int c)
Definition: utclib.c:902
int(* FARPROC)()
Definition: compat.h:28

Referenced by START_TEST().

◆ ExceptionFilter()

LONG ExceptionFilter ( IN PEXCEPTION_POINTERS  ExceptionInfo,
ULONG  ExceptionCode 
)

Definition at line 305 of file delayimp.cpp.

306 {
308  ok(ExceptionCode == expected, "Expected code to be 0x%lx, was: 0x%lx\n", expected, ExceptionCode);
309  ok(ExceptionInfo != NULL, "Expected to get exception info\n");
310  ok(ExceptionInfo->ExceptionRecord != NULL, "Expected to get a valid record info\n");
311 
312  if (ExceptionCode != expected)
313  {
314  skip("Skipping other checks, this was not the exception we expected!\n");
316  }
317 
318  if (ExceptionInfo && ExceptionInfo->ExceptionRecord)
319  {
320  PEXCEPTION_RECORD ExceptionRecord = ExceptionInfo->ExceptionRecord;
321  ok(ExceptionRecord->ExceptionCode == expected, "Expected ExceptionCode to be 0x%lx, was 0x%lx\n",
322  expected, ExceptionRecord->ExceptionCode);
323  /* We can still continue. */
324  ok(ExceptionRecord->ExceptionFlags == 0, "Expected ExceptionFlags to be 0, was: 0x%lx\n",
325  ExceptionRecord->ExceptionFlags);
326  ok(ExceptionRecord->NumberParameters == 1, "Expected 1 parameter, got %lu\n",
327  ExceptionRecord->NumberParameters);
328  if (ExceptionRecord->NumberParameters == 1)
329  {
330  PDelayLoadInfo LoadInfo = (PDelayLoadInfo)ExceptionRecord->ExceptionInformation[0];
331  ok(LoadInfo && LoadInfo->cb >= 36, "Expected a valid pointer with a struct that is big enough: %p, %lu\n",
332  LoadInfo, LoadInfo ? LoadInfo->cb : 0);
333 
334  if (g_ExpectedDll)
335  ok(!strcmp(g_ExpectedDll, LoadInfo->szDll), "Expected szDll to be '%s', but was: '%s'\n",
336  g_ExpectedDll, LoadInfo->szDll);
337  if (g_ExpectedName)
338  {
339  ok(LoadInfo->dlp.fImportByName, "Expected import by name\n");
340  if (LoadInfo->dlp.fImportByName)
341  {
342  ok(!strcmp(g_ExpectedName, LoadInfo->dlp.szProcName),
343  "Expected szProcName to be '%s', but was: '%s'\n", g_ExpectedName, LoadInfo->dlp.szProcName);
344 
346  {
347  HMODULE mod = LoadLibraryA("imagehlp.dll");
348  LoadInfo->pfnCur = GetProcAddress(mod, g_ExpectedName);
349  }
350  else
351  {
352  char buf[100];
354  sprintf(buf, "%c%s", first, g_ExpectedName + 1);
356  }
358  }
359  }
360  }
361  }
362 
364 }
FARPROC pfnCur
Definition: delayimp.h:73
_Inout_ PIRP _In_ NTSTATUS ExceptionCode
Definition: cdprocs.h:1782
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const GLint * first
Definition: glext.h:5794
struct DelayLoadInfo * PDelayLoadInfo
BOOL fImportByName
Definition: delayimp.h:57
bool g_ExceptionIsModule
Definition: delayimp.cpp:42
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD ExceptionCode
Definition: compat.h:196
#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
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
DelayLoadProc dlp
Definition: delayimp.h:71
smooth NULL
Definition: ftsmooth.c:416
int toupper(int c)
Definition: utclib.c:881
ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]
Definition: compat.h:201
#define isupper(c)
Definition: acclib.h:71
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD cb
Definition: delayimp.h:67
LPCSTR szDll
Definition: delayimp.h:70
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:821
struct _EXCEPTION_RECORD * ExceptionRecord
Definition: compat.h:198
#define ERROR_PROC_NOT_FOUND
Definition: winerror.h:199
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
const char * g_ExpectedName
Definition: delayimp.cpp:45
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:94
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
DWORD ExceptionFlags
Definition: compat.h:197
#define GetProcAddress(x, y)
Definition: compat.h:418
#define VcppException(sev, err)
Definition: delayimp.cpp:30
DWORD NumberParameters
Definition: compat.h:200
const char * g_ExpectedDll
Definition: delayimp.cpp:44
int tolower(int c)
Definition: utclib.c:902
BOOL expected
Definition: store.c:2063
static int mod
Definition: i386-dis.c:1273
#define EXCEPTION_CONTINUE_EXECUTION
Definition: excpt.h:87

Referenced by START_TEST().

◆ MyFunction()

INT_PTR WINAPI MyFunction ( )

Definition at line 131 of file delayimp.cpp.

132 {
133  return 123;
134 }

Referenced by DliHook().

◆ MySfcIsKeyProtected()

BOOL WINAPI MySfcIsKeyProtected ( HKEY  KeyHandle,
LPCWSTR  SubKeyName,
REGSAM  KeySam 
)

Definition at line 136 of file delayimp.cpp.

137 {
138  return 12345;
139 }

Referenced by DliHook().

◆ Rva2Addr()

template<typename PTR >
PTR Rva2Addr ( PIMAGE_DOS_HEADER  dos,
RVA  rva 
)

Definition at line 378 of file delayimp.cpp.

379 {
380  /* Old delayload type */
381  if (g_UsePointers)
382  return reinterpret_cast<PTR>(rva);
383  return reinterpret_cast<PTR>((reinterpret_cast<PBYTE>(dos) + rva));
384 }
bool g_UsePointers
Definition: delayimp.cpp:375
IMAGE_DOS_HEADER dos
Definition: module.c:49

◆ SetExpectedDli()

static void SetExpectedDli ( unsigned order)
static

Definition at line 88 of file delayimp.cpp.

89 {
91  g_DliHookIndex = 0;
92  g_Target[0] = '\0';
93 }
GLuint GLdouble GLdouble GLint GLint order
Definition: glext.h:11194
char g_Target[100]
Definition: delayimp.cpp:46
size_t g_DliHookIndex
Definition: delayimp.cpp:85
unsigned * g_DliHookExpected
Definition: delayimp.cpp:84

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( delayimp  )

Definition at line 400 of file delayimp.cpp.

401 {
402  /* Verify that both scenario's work */
403  ok(__pfnDliNotifyHook2 == DliHook, "Expected __pfnDliNotifyHook2 to be DliHook(%p), but was: %p\n",
405  ok(__pfnDliFailureHook2 == NULL, "Expected __pfnDliFailureHook2 to be NULL, but was: %p\n",
407 
409 
410 
412 
413  ok(dos->e_magic == IMAGE_DOS_SIGNATURE, "Expected a DOS header\n");
415  return;
416 
419 
420  /* Test some advanced features (loading / unloading) */
421  if (delaydir->Size != 0)
422  {
423 #if defined(_DELAY_IMP_VER) && _DELAY_IMP_VER == 2 && defined(DELAYLOAD_SUPPORTS_UNLOADING)
424  /* First, before mangling the delayload stuff, let's try some v2 functions */
426  ok(mod == NULL, "Expected mod to be NULL, was %p\n", mod);
427  /* Now, a mistyped module (case sensitive!) */
428  HRESULT hr = __HrLoadAllImportsForDll("WiNmM.DlL");
429  ok(hr == HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), "Expected hr to be HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND), was %lu\n", hr);
431  ok(mod == NULL, "Expected mod to be NULL, was %p\n", mod);
432 
433  /* Let's load it */
434  hr = __HrLoadAllImportsForDll(WINMM_DLLNAME);
435  ok(hr == S_OK, "Expected hr to be S_OK, was %lu\n", hr);
437  ok(mod != NULL, "Expected mod to be valid, was NULL\n");
438 
439  BOOL status = __FUnloadDelayLoadedDLL2(WINMM_DLLNAME);
440  ok(status == TRUE, "Expected __FUnloadDelayLoadedDLL2 to succeed\n");
442  ok(mod == NULL, "Expected mod to be NULL, was %p\n", mod);
443 #else
444  trace("Binary compiled without support for unloading\n");
445 #endif
446  }
447  else
448  {
449  skip("No IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT found, some advanced features might not work!\n");
450  }
451 
452  /* Test the normal flow without a dll loaded */
455  g_ExpectedName = "mmGetCurrentTask";
456  DWORD task = mmGetCurrentTask();
457  ok(task == GetCurrentThreadId(), "Expected ret to be current thread id (0x%lx), was 0x%lx\n", GetCurrentThreadId(), task);
458  CheckDliDone();
459 
460  /* Test the normal flow with a dll loaded */
463  g_ExpectedName = "midiOutClose";
464  DWORD err = midiOutClose((HMIDIOUT)(ULONG_PTR)0xdeadbeef);
465  ok(err == MMSYSERR_INVALHANDLE, "Expected err to be MMSYSERR_INVALHANDLE, was 0x%lx\n", err);
466  CheckDliDone();
467 
468  /* Make sure GetProcAddress fails, also ignore the Failure hook, use the exception to set the address */
471  g_ExpectedName = "MixerClose";
472  g_BreakFunctionName = true;
473  _SEH2_TRY
474  {
475  err = mixerClose((HMIXER)(ULONG_PTR)0xdeadbeef);
476  }
478  {
480  }
481  _SEH2_END;
482  ok(err == MMSYSERR_INVALHANDLE, "Expected err to be MMSYSERR_INVALHANDLE, was 0x%lx\n", err);
483  CheckDliDone();
484  ok(g_BreakFunctionName == false, "Expected the functionname to be changed\n");
485 
486  /* Make the LoadLib fail, manually load the library in the Failure Hook,
487  Respond to the dliNotePreGetProcAddress with an alternate function address */
490  ok(ret == 12345, "Expected ret to be 12345, was %u\n", ret); /* The original function returns FALSE! */
491  CheckDliDone();
492 
493  /* Show that it works with the manually returned dll */
496  ok(ret == FALSE, "Expected ret to be FALSE, was %u\n", ret);
497  CheckDliDone();
498 
499  /* Return a fake dll handle, so that we can see when it is being used, and manually return a function in the Failure Hook */
502  ok(ret == FALSE, "Expected ret to be FALSE, was %u\n", ret);
503  CheckDliDone();
504 
505  /* Manually return a function in the failure hook, when the module is the previously set bad one */
508  ok(ret == FALSE, "Expected ret to be FALSE, was %u\n", ret);
509  CheckDliDone();
510 
511  if (HIWORD(SymGetOptions) == NULL)
512  {
513  skip("SymGetOptions until CORE-6504 is fixed\n");
514  }
515  else
516  {
517  /* Completely bypass most hooks, by directly replying with a function address */
519  g_BypassMost = true;
520  DWORD opt = SymGetOptions();
521  g_BypassMost = false;
522  ok(opt == 123, "Expected opt to be 123, was %lu\n", opt); /* The original function returns ERROR_INVALID_HANDLE */
523  CheckDliDone();
524  }
525 
526  /* Import by ordinal */
527  g_ImportByName = false;
529  PARSEDURLA pua = { sizeof(pua), 0 };
530  HRESULT hr = ParseURLA("", &pua);
531  ok(hr == URL_E_INVALID_SYNTAX, "Expected tmp to be URL_E_INVALID_SYNTAX, was %lx\n", hr);
532  CheckDliDone();
533  g_ImportByName = true;
534 
535  /* Handle LoadLib failure with an exception handler */
536  if (HIWORD(MapAndLoad) == NULL)
537  {
538  skip("MapAndLoad until CORE-6504 is fixed\n");
539  }
540  else
541  {
543  LOADED_IMAGE img = {0};
544  ret = 123;
545  g_ExceptionIsModule = true;
546  g_ExpectedDll = "xxxxxhlp.dll";
547  g_ExpectedName = "MapAndLoad";
548  _SEH2_TRY
549  {
550  ret = MapAndLoad("some_not_existing_file.aabbcc", NULL, &img, FALSE, TRUE);
551  }
553  {
554  ;
555  }
556  _SEH2_END;
557  g_ExceptionIsModule = false;
558  ok(ret == FALSE, "Expected ret to be FALSE, was %u\n", ret);
559  CheckDliDone();
560  }
561 }
#define TRUE
Definition: types.h:120
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
unsigned g_winmm_get_cur_task[]
Definition: delayimp.cpp:387
HRESULT hr
Definition: shlfolder.c:183
static void SetExpectedDli(unsigned *order)
Definition: delayimp.cpp:88
unsigned g_imagehlp[]
Definition: delayimp.cpp:396
#define CheckDliDone
Definition: delayimp.cpp:127
UINT WINAPI mixerClose(HMIXER hMix)
Definition: winmm.c:389
HRESULT WINAPI ParseURLA(LPCSTR x, PARSEDURLA *y)
Definition: url.c:161
#define WINMM_DLLNAME
Definition: delayimp.cpp:36
bool g_BypassMost
Definition: delayimp.cpp:41
unsigned g_winmm_mide_in_close[]
Definition: delayimp.cpp:389
unsigned g_sfc_key[]
Definition: delayimp.cpp:390
#define MMSYSERR_INVALHANDLE
Definition: mmsystem.h:101
unsigned g_version_w[]
Definition: delayimp.cpp:393
IMAGE_NT_HEADERS nt
Definition: module.c:50
BOOL WINAPI MapAndLoad(PCSTR pszImageName, PCSTR pszDllPath, PLOADED_IMAGE pLoadedImage, BOOL bDotDll, BOOL bReadOnly)
Definition: access.c:131
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
unsigned g_version_a[]
Definition: delayimp.cpp:392
FARPROC WINAPI DliFailHook(unsigned dliNotify, PDelayLoadInfo pdli)
Definition: delayimp.cpp:256
_SEH2_TRY
Definition: create.c:4250
uint32_t ULONG_PTR
Definition: typedefs.h:63
bool g_ExceptionIsModule
Definition: delayimp.cpp:42
unsigned g_sfc_file[]
Definition: delayimp.cpp:391
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
LONG ExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo, ULONG ExceptionCode)
Definition: delayimp.cpp:305
unsigned g_shlwapi[]
Definition: delayimp.cpp:395
bool g_ImportByName
Definition: delayimp.cpp:43
unsigned int BOOL
Definition: ntddk_ex.h:94
#define _SEH2_GetExceptionInformation()
Definition: pseh2_64.h:11
smooth NULL
Definition: ftsmooth.c:416
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
#define URL_E_INVALID_SYNTAX
Definition: intshcut.h:32
DWORD WINAPI GetCurrentThreadId(VOID)
Definition: thread.c:458
unsigned g_scard[]
Definition: delayimp.cpp:394
#define trace
Definition: atltest.h:70
GLint GLvoid * img
Definition: gl.h:1956
LONG HRESULT
Definition: typedefs.h:77
UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
Definition: winmm.c:981
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI SfcIsKeyProtected(HKEY hKey, LPCWSTR lpSubKey, REGSAM samDesired)
Definition: sfc_os.c:117
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
Definition: ntimage.h:488
unsigned g_winmm_midi_out_close[]
Definition: delayimp.cpp:388
BOOL WINAPI GetFileVersionInfoA(LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data)
Definition: version.c:853
int ret
IMAGE_DOS_HEADER dos
Definition: module.c:49
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:821
#define err(...)
FARPROC WINAPI DliHook(unsigned dliNotify, PDelayLoadInfo pdli)
Definition: delayimp.cpp:143
PfnDliHook __pfnDliNotifyHook2
Definition: delayimp.cpp:370
PfnDliHook __pfnDliFailureHook2
Definition: pdb.c:221
#define S_OK
Definition: intsafe.h:59
_SEH2_END
Definition: create.c:4424
#define GetModuleHandle
Definition: winbase.h:3661
#define ok(value,...)
Definition: atltest.h:57
bool g_BreakFunctionName
Definition: delayimp.cpp:39
BOOL WINAPI SfcGetNextProtectedFile(HANDLE RpcHandle, PPROTECTED_FILE_DATA ProtFileData)
Definition: sfc_os.c:142
DWORD WINAPI SymGetOptions(void)
Definition: dbghelp.c:458
BOOL WINAPI GetFileVersionInfoW(LPCWSTR filename, DWORD handle, DWORD datasize, LPVOID data)
Definition: version.c:845
#define skip(...)
Definition: atltest.h:64
const char * g_ExpectedName
Definition: delayimp.cpp:45
#define ERROR_MOD_NOT_FOUND
Definition: compat.h:94
#define HIWORD(l)
Definition: typedefs.h:246
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
DWORD APIENTRY mmGetCurrentTask(VOID)
Definition: winmm.c:2915
const char * g_ExpectedDll
Definition: delayimp.cpp:44
BYTE * PBYTE
Definition: pedump.c:66
static int mod
Definition: i386-dis.c:1273
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
Definition: ps.c:97

◆ target()

Definition at line 48 of file delayimp.cpp.

49 {
50  if (g_Target[0] == '\0' && pdli)
51  {
52  if (pdli->dlp.fImportByName)
53  sprintf(g_Target, "%s!%s", pdli->szDll, pdli->dlp.szProcName);
54  else
55  sprintf(g_Target, "%s!#%lu", pdli->szDll, pdli->dlp.dwOrdinal);
56  }
57  return g_Target;
58 }
char g_Target[100]
Definition: delayimp.cpp:46
BOOL fImportByName
Definition: delayimp.h:57
#define sprintf(buf, format,...)
Definition: sprintf.c:55
LPCSTR szProcName
Definition: delayimp.h:60
DelayLoadProc dlp
Definition: delayimp.h:71
LPCSTR szDll
Definition: delayimp.h:70
DWORD dwOrdinal
Definition: delayimp.h:61

Referenced by CheckDli_imp(), and CheckDliDone_imp().

Variable Documentation

◆ __pfnDliNotifyHook2

PfnDliHook __pfnDliNotifyHook2 = DliHook

Definition at line 370 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_BreakFunctionName

bool g_BreakFunctionName = false

Definition at line 39 of file delayimp.cpp.

Referenced by DliHook(), and START_TEST().

◆ g_BrokenFunctionName

bool g_BrokenFunctionName = false

Definition at line 40 of file delayimp.cpp.

Referenced by DliHook().

◆ g_BypassMost

bool g_BypassMost = false

Definition at line 41 of file delayimp.cpp.

Referenced by DliHook(), and START_TEST().

◆ g_DliHookExpected

unsigned* g_DliHookExpected = NULL

Definition at line 84 of file delayimp.cpp.

Referenced by CheckDli_imp(), CheckDliDone_imp(), and SetExpectedDli().

◆ g_DliHookIndex

size_t g_DliHookIndex = 0

Definition at line 85 of file delayimp.cpp.

Referenced by CheckDli_imp(), CheckDliDone_imp(), and SetExpectedDli().

◆ g_ExceptionIsModule

bool g_ExceptionIsModule = false

Definition at line 42 of file delayimp.cpp.

Referenced by ExceptionFilter(), and START_TEST().

◆ g_ExpectedDll

const char* g_ExpectedDll = NULL

Definition at line 44 of file delayimp.cpp.

Referenced by DliHook(), ExceptionFilter(), and START_TEST().

◆ g_ExpectedName

const char* g_ExpectedName = NULL

Definition at line 45 of file delayimp.cpp.

Referenced by DliHook(), ExceptionFilter(), and START_TEST().

◆ g_imagehlp

Definition at line 396 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_ImportByName

bool g_ImportByName = true

Definition at line 43 of file delayimp.cpp.

Referenced by DliHook(), and START_TEST().

◆ g_scard

Definition at line 394 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_sfc_file

Definition at line 391 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_sfc_key

◆ g_shlwapi

◆ g_Target

char g_Target[100] = { 0 }

Definition at line 46 of file delayimp.cpp.

Referenced by CheckDliDone_imp(), SetExpectedDli(), and target().

◆ g_UsePointers

bool g_UsePointers = false

Definition at line 375 of file delayimp.cpp.

Referenced by Rva2Addr().

◆ g_version_a

◆ g_version_w

◆ g_VersionDll

HMODULE g_VersionDll
static

Definition at line 142 of file delayimp.cpp.

Referenced by DliFailHook(), and DliHook().

◆ g_winmm_get_cur_task

Definition at line 387 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_winmm_mide_in_close

Definition at line 389 of file delayimp.cpp.

Referenced by START_TEST().

◆ g_winmm_midi_out_close

Definition at line 388 of file delayimp.cpp.

Referenced by START_TEST().