Listing 3: DELAYIMP.H
Delay-loading support and hook functions
/* DELAYIMP.H - delay-loading support and hook functions */ #pragma once #if !defined __DELAYIMP_H #define __DELAYIMP_H #if defined __cplusplus extern "C" { #endif /* The ImgDelayDescr structure defines the raw format of internal tables * that the linker generates for each delay-loaded DLL. */ typedef const struct ImgDelayDescr *PCImgDelayDescr; typedef struct DelayLoadProc { BOOL fImportByName; union { LPCSTR szProcName; DWORD dwOrdinal; }; } DelayLoadProc; typedef struct DelayLoadInfo { DWORD cb; /* size of the structure */ PCImgDelayDescr pidd; /* raw form of the data */ FARPROC * ppfn; /* points to address of function to import */ LPCSTR szDll; /* filename of the DLL */ DelayLoadProc dlp; /* name or ordinal of function to import */ HMODULE hmodCur; /* module handle of the DLL after loading it */ FARPROC pfnCur; /* address of imported function */ DWORD dwLastError; /* error received (in an error notification) */ } DelayLoadInfo, * PDelayLoadInfo; typedef FARPROC (WINAPI *PfnDliHook)(unsigned dliNotify, PDelayLoadInfo pdli); /* Unload support: unload a specific delay-loaded DLL or unload all * delay-loaded DLLs by setting "szDll" to NULL. */ BOOL WINAPI __FUnloadDelayLoadedDLL(LPCSTR szDll); /* Structured Exception Handling information */ #if !defined ERROR_MOD_NOT_FOUND #define ERROR_MOD_NOT_FOUND 126L /* from WINERROR.H */ #endif #if !defined ERROR_PROC_NOT_FOUND #define ERROR_PROC_NOT_FOUND 127L /* from WINERROR.H */ #endif #if defined __BORLANDC__ #define FACILITY_DELAYLOAD 251L #else /* Microsoft Visual C/C++ */ #define FACILITY_DELAYLOAD 109L #endif #define VcppException(sev,err) ((sev) | (FACILITY_DELAYLOAD<<16) | err) /* Delay-load notification and failure hooks * * The "notification hook" function is called for every function to import * (but only once per function). The dliNotePreLoadLibrary notification is * only sent if the DLL is not already loaded. The "failure hook" function * gets called if a LoadLibrary() or GetProcAddress() fails. The failure * hook may then try to fix the situation and return the requested handle * or address. * * __pfnDliNotifyHook receives: * dliStartProcessing * dliNotePreLoadLibrary * dliNotePreGetProcAddress * dliNoteEndProcessing * * __pfnDliFailureHook receives: * dliFailLoadLib * dliFailGetProc */ #if defined __BORLANDC__ /* C++ Builder 5 contains the hook variables in both a static link library * and a DLL RTL. */ #define DeclSpec __declspec(dllimport) #else /* Microsoft Visual C/C++ has only a static linked lib for delay-loading */ #define DeclSpec #endif enum { dliStartProcessing, /* start import of function; may return FARPROC */ dliNotePreLoadLibrary, /* pre-LoadLibrary; may return HMODULE */ dliNotePreGetProcAddress, /* pre-GetProcAddress; may return FARPROC */ dliFailLoadLib, /* LoadLibrary-failure; may return HMODULE */ dliFailGetProc, /* GetProcAddress-failure; may return FARPROC */ dliNoteEndProcessing, /* done importing a function */ }; DeclSpec extern PfnDliHook __pfnDliNotifyHook; DeclSpec extern PfnDliHook __pfnDliFailureHook; #undef DeclSpec #if defined __cplusplus } /* extern "C" */ #endif #endif /* !defined __DELAYIMP_H */