Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Packing DLLs in your EXE


June 2002/ Packing DLLs in your EXE

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 */


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.