home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / delayimp.h < prev    next >
C/C++ Source or Header  |  1998-05-05  |  5KB  |  151 lines

  1. //
  2. // DelayImp.h
  3. //
  4. //  define structures and prototypes necessary for delay loading of imports
  5. //
  6. #pragma once
  7. #if !defined(_delayimp_h)
  8. #define _delayimp_h
  9.  
  10. #if defined(__cplusplus)
  11. #define ExternC extern "C"
  12. #else
  13. #define ExternC
  14. #endif
  15.  
  16. typedef IMAGE_THUNK_DATA *          PImgThunkData;
  17. typedef const IMAGE_THUNK_DATA *    PCImgThunkData;
  18.  
  19. typedef struct ImgDelayDescr {
  20.     DWORD           grAttrs;        // attributes
  21.     LPCSTR          szName;         // pointer to dll name
  22.     HMODULE *       phmod;          // address of module handle
  23.     PImgThunkData   pIAT;           // address of the IAT
  24.     PCImgThunkData  pINT;           // address of the INT
  25.     PCImgThunkData  pBoundIAT;      // address of the optional bound IAT
  26.     PCImgThunkData  pUnloadIAT;     // address of optional copy of original IAT
  27.     DWORD           dwTimeStamp;    // 0 if not bound,
  28.                                     // O.W. date/time stamp of DLL bound to (Old BIND)
  29.     } ImgDelayDescr, * PImgDelayDescr;
  30.  
  31. typedef const ImgDelayDescr *   PCImgDelayDescr;
  32.  
  33. //
  34. // Delay load import hook notifications
  35. //
  36. enum {
  37.     dliStartProcessing,             // used to bypass or note helper only
  38.     dliNotePreLoadLibrary,          // called just before LoadLibrary, can
  39.                                     //  override w/ new HMODULE return val
  40.     dliNotePreGetProcAddress,       // called just before GetProcAddress, can
  41.                                     //  override w/ new FARPROC return value
  42.     dliFailLoadLib,                 // failed to load library, fix it by
  43.                                     //  returning a valid HMODULE
  44.     dliFailGetProc,                 // failed to get proc address, fix it by
  45.                                     //  returning a valid FARPROC
  46.     dliNoteEndProcessing,           // called after all processing is done, no
  47.                                     //  no bypass possible at this point except
  48.                                     //  by longjmp()/throw()/RaiseException.
  49.     };
  50.  
  51. typedef struct DelayLoadProc {
  52.     BOOL                fImportByName;
  53.     union {
  54.         LPCSTR          szProcName;
  55.         DWORD           dwOrdinal;
  56.         };
  57.     } DelayLoadProc;
  58.  
  59. typedef struct DelayLoadInfo {
  60.     DWORD               cb;         // size of structure
  61.     PCImgDelayDescr     pidd;       // raw form of data (everything is there)
  62.     FARPROC *           ppfn;       // points to address of function to load
  63.     LPCSTR              szDll;      // name of dll
  64.     DelayLoadProc       dlp;        // name or ordinal of procedure
  65.     HMODULE             hmodCur;    // the hInstance of the library we have loaded
  66.     FARPROC             pfnCur;     // the actual function that will be called
  67.     DWORD               dwLastError;// error received (if an error notification)
  68.     } DelayLoadInfo, * PDelayLoadInfo;
  69.  
  70. typedef FARPROC (WINAPI *PfnDliHook)(
  71.     unsigned        dliNotify,
  72.     PDelayLoadInfo  pdli
  73.     );
  74.  
  75. // utility function for calculating the index of the current import
  76. // for all the tables (INT, BIAT, UIAT, and IAT).
  77. __inline unsigned
  78. IndexFromPImgThunkData(PCImgThunkData pitdCur, PCImgThunkData pitdBase) {
  79.     return pitdCur - pitdBase;
  80.     }
  81.  
  82. //
  83. // Unload support
  84. //
  85.  
  86. // routine definition; takes a pointer to a name to unload, or NULL to
  87. // unload all the delay load dlls in the list.
  88. //
  89. ExternC
  90. BOOL WINAPI
  91. __FUnloadDelayLoadedDLL(LPCSTR szDll);
  92.  
  93. // structure definitions for the list of unload records
  94. typedef struct UnloadInfo * PUnloadInfo;
  95. typedef struct UnloadInfo {
  96.     PUnloadInfo     puiNext;
  97.     PCImgDelayDescr pidd;
  98.     } UnloadInfo;
  99.  
  100. // the default delay load helper places the unloadinfo records in the list
  101. // headed by the following pointer.
  102. ExternC
  103. extern
  104. PUnloadInfo __puiHead;
  105.  
  106. //
  107. // Exception information
  108. //
  109. #define FACILITY_VISUALCPP  ((LONG)0x6d)
  110. #define VcppException(sev,err)  ((sev) | (FACILITY_VISUALCPP<<16) | err)
  111.  
  112. // utility function for calculating the count of imports given the base
  113. // of the IAT.  NB: this only works on a valid IAT!
  114. __inline unsigned
  115. CountOfImports(PCImgThunkData pitdBase) {
  116.     unsigned        cRet = 0;
  117.     PCImgThunkData  pitd = pitdBase;
  118.     while (pitd->u1.Function) {
  119.         pitd++;
  120.         cRet++;
  121.         }
  122.     return cRet;
  123.     }
  124.  
  125. //
  126. // Hook pointers
  127. //
  128.  
  129. // The "notify hook" gets called for every call to the
  130. // delay load helper.  This allows a user to hook every call and
  131. // skip the delay load helper entirely.
  132. //
  133. // dliNotify == {
  134. //  dliStartProcessing |
  135. //  dliPreLoadLibrary  |
  136. //  dliPreGetProc |
  137. //  dliNoteEndProcessing}
  138. //  on this call.
  139. //
  140. ExternC
  141. extern
  142. PfnDliHook   __pfnDliNotifyHook;
  143.  
  144. // This is the failure hook, dliNotify = {dliFailLoadLib|dliFailGetProc}
  145. ExternC
  146. extern
  147. PfnDliHook   __pfnDliFailureHook;
  148.  
  149.  
  150. #endif
  151.