home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / atonexit.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  173 lines

  1. /***
  2. *atonexit.c - _onexit/atexit for using the MSVCRT* model of C run-time
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       atexit and _onexit are handled differently for EXE's and DLL's linked
  8. *       with MSVCRT.LIB to call MSVCRT*.DLL.  Specifically, the _onexit/atexit
  9. *       list for DLL's must be maintained on a per-module basis and must be
  10. *       processed at DLL process detach .  For EXE's the atexit/_onexit list
  11. *       should be maintained by MSVCRT*.DLL and processed at process exit.
  12. *
  13. *******************************************************************************/
  14.  
  15. #ifndef _MAC
  16.  
  17. /*
  18.  * SPECIAL BUILD MACRO! Note that atonexit.c is linked in with the client's
  19.  * code. It does not go into crtdll.dll! Therefore, it must be built under
  20.  * the _DLL switch (like user code) and CRTDLL must be undefined.
  21.  */
  22.  
  23. #undef  CRTDLL
  24. #define _DLL
  25.  
  26. #include <cruntime.h>
  27. #include <oscalls.h>
  28. #include <internal.h>
  29. #include <stdlib.h>
  30.  
  31. /*
  32.  * routine in DLL to do initialization (in this case, C++ constructors)
  33.  */
  34.  
  35. extern void __cdecl _initterm(_PVFV *, _PVFV *);
  36.  
  37.  
  38. /*
  39.  * Pointers to beginning and end of the table of function pointers manipulated
  40.  * by _onexit()/atexit().  If this module is an EXE, _onexitbegin will be -1.
  41.  * Otherwise _onexitbegin will point to a block of malloc-ed memory used to
  42.  * maintain the DLL module's private onexit list of terminator routines.
  43.  */
  44.  
  45. _PVFV *__onexitbegin;
  46. _PVFV *__onexitend;
  47.  
  48.  
  49. /***
  50. *_onexit, atexit - calls to _onexit & atexit in MSVCRT*.DLL
  51. *
  52. *Purpose:
  53. *       A DLL linked with MSVCRT.LIB must not call the standard _onexit or
  54. *       atexit exported from MSVCRT*.DLL, but an EXE linked with MSVCRT.LIB
  55. *       will call the standard versions of those two routines.  The standard
  56. *       names are not exported from MSVCRT*.DLL, but the _imp__* names are,
  57. *       so this module can just invoke the standard versions if linked into
  58. *       an EXE module (indicated by __onexitbegin == -1).  If this module is
  59. *       linked into a DLL (indicated by __onexitbegin != -1), it will call a
  60. *       helper routine in MSVCRT*.DLL called __dllonexit to manager the DLL's
  61. *       private onexit list.
  62. *
  63. *Entry:
  64. *       Same as the regular versions of _onexit, atexit.
  65. *
  66. *Exit:
  67. *       Same as the regular versions of _onexit, atexit.
  68. *
  69. *Exceptions:
  70. *
  71. *******************************************************************************/
  72.  
  73. extern _onexit_t __dllonexit(_onexit_t, _PVFV**, _PVFV**);
  74.  
  75. #ifdef _M_IX86
  76. extern _onexit_t (__cdecl *_imp___onexit)(_onexit_t func);
  77. #else  /* _M_IX86 */
  78. extern _onexit_t (__cdecl *__imp__onexit)(_onexit_t func);
  79. #endif  /* _M_IX86 */
  80.  
  81. _onexit_t __cdecl _onexit (
  82.         _onexit_t func
  83.         )
  84. {
  85. #ifdef _M_IX86
  86.  
  87.         return( (__onexitbegin == (_PVFV *) -1)
  88.              /* EXE */ ? (*_imp___onexit)(func)
  89.              /* DLL */ : __dllonexit(func, &__onexitbegin, &__onexitend));
  90.  
  91. #else  /* _M_IX86 */
  92.  
  93.         return( (__onexitbegin == (_PVFV *) -1)
  94.             /* EXE */ ? (*__imp__onexit)(func)
  95.             /* DLL */ : __dllonexit(func, &__onexitbegin, &__onexitend) );
  96.  
  97. #endif  /* _M_IX86 */
  98. }
  99.  
  100. int __cdecl atexit (
  101.         _PVFV func
  102.         )
  103. {
  104.         return (_onexit((_onexit_t)func) == NULL) ? -1 : 0;
  105. }
  106.  
  107. #else  /* _MAC */
  108.  
  109.  
  110. #undef  CRTDLL
  111. #define _DLL
  112.  
  113. #include <cruntime.h>
  114. #include <malloc.h>
  115. #include <internal.h>
  116. #include <stdlib.h>
  117. #include <fltintrn.h>
  118.  
  119. /*
  120.  * routines in DLL to do onexit calls for EXE and DLL, respectively
  121.  */
  122.  
  123. extern _onexit_t _cdecl __onexit(_onexit_t);
  124. extern _onexit_t _cdecl __dllonexit(_onexit_t, PFV**, PFV**);
  125.  
  126. /*
  127.  * Pointers to beginning and end of the table of function pointers manipulated
  128.  * by _onexit()/atexit().  If this module is an EXE, _onexitbegin will be -1.
  129.  * Otherwise _onexitbegin will point to a block of malloc-ed memory used to
  130.  * maintain the DLL module's private onexit list of terminator routines.
  131.  */
  132.  
  133. PFV *__onexitbegin;
  134. PFV *__onexitend;
  135.  
  136. /***
  137. *_onexit, atexit - calls to _onexit & atexit in MSVCRT*.DLL
  138. *
  139. *Purpose:
  140. *       A DLL linked with MSVCRT.LIB must not call the standard _onexit or
  141. *       atexit exported from MSVCRT*.DLL, and an EXE linked with MSVCRT.LIB
  142. *       will this version too.  The standard
  143. *       names are not exported from MSVCRT*.DLL.
  144. *
  145. *Entry:
  146. *       Same as the regular versions of _onexit, atexit.
  147. *
  148. *Exit:
  149. *       Same as the regular versions of _onexit, atexit.
  150. *
  151. *Exceptions:
  152. *
  153. *******************************************************************************/
  154.  
  155. _onexit_t __cdecl _onexit (
  156.         _onexit_t func
  157.         )
  158. {
  159.         return( (__onexitbegin == (PFV *) -1)
  160.             /* EXE */ ? (__onexit)(func)
  161.             /* DLL */ : __dllonexit(func, &__onexitbegin, &__onexitend) );
  162. }
  163.  
  164.  
  165. int __cdecl atexit (
  166.         PFV func
  167.         )
  168. {
  169.         return (_onexit((_onexit_t)func) == NULL) ? -1 : 0;
  170. }
  171.  
  172. #endif  /* _MAC */
  173.