home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / open32 / dllentry / dllmain.c
C/C++ Source or Header  |  1999-03-15  |  5KB  |  96 lines

  1. /*****************************************************************************/
  2. /* Copyright:                                                                */
  3. /*   Licensed Materials - Property of IBM                                    */
  4. /*   (C) Copyright IBM Corp. 1995                                            */
  5. /*   All Rights Reserved                                                     */
  6. /*****************************************************************************/
  7. /* File: dllmain.c                                                           */
  8. /*****************************************************************************/
  9. /* Description:                                                              */
  10. /*   This file contains the DLL initialization/Termination function, which   */
  11. /*   is called when a process gains or loses access to the DLL.  The DEF     */
  12. /*   file used to build the DLL needs to specify INITINSTANCE and            */
  13. /*   TERMINSTANCE, otherwise this function will only be called for the       */
  14. /*   first process to gain access and the last process to free up the DLL.   */
  15. /*                                                                           */
  16. /*   This implementation is for IBM C/Set++ and assumes that the 'C'         */
  17. /*   Runtime library is being statically linked to the DLL and that the      */
  18. /*   library uses C++ classes.                                               */
  19. /*                                                                           */
  20. /* Restrictions:                                                             */
  21. /*   DLL_THREAD_ATTACH/DETACH is not supported. DisableThreadLibraryCalls()  */
  22. /*   is also not supported.                                                  */
  23. /*                                                                           */
  24. /*   lpvReserved always indicates that the DLL was loaded statically even    */
  25. /*   when the DLL was dynamically loaded.                                    */
  26. /*                                                                           */
  27. /*   If DllEntryPoint is used, HAVE_DLLENTRYPOINT needs to be defined        */
  28. /*   during compilation of this file.                                        */
  29. /*****************************************************************************/
  30. #include <os2win.h>
  31.  
  32.  
  33. int _CRT_init(void);
  34. void _CRT_term(void);
  35. void __ctordtorInit(void);
  36. void __ctordtorTerm(void);
  37. void _System WinInitializeOpen32(ULONG);
  38. void _System WinTerminateOpen32(ULONG);
  39.  
  40. #ifdef _DEKKO_
  41.  #pragma map (Dekko32Trace,"_DEKKO32TRACE")
  42.  #include <dekko32.h>
  43. #else
  44.   #define Dekko32Trace
  45. #endif /* _DEKKO_ */
  46.  
  47. unsigned long _System _DLL_InitTerm(unsigned long handle, unsigned long flag)
  48. {
  49.  
  50.    unsigned long reserved;
  51.  
  52.   Dekko32Trace(0xAA00,757,0,NULL);
  53.  
  54.    if ( flag )
  55.    {
  56.       /* Termination: A process is losing access to this DLL */
  57.  
  58.    #ifdef HAVE_DLLENTRYPOINT
  59.       /* Call Win32 Initialization/Termination function                 */
  60.       /* (NOTE: this assumes the DLL entry point is a function called   */
  61.       /*        DllEntryPoint)                                          */
  62.       DllEntryPoint((HANDLE) handle, DLL_PROCESS_DETACH, &reserved);
  63.    #endif
  64.  
  65.       __ctordtorTerm();                      /* Termination code for C++ Objects */
  66.       _CRT_term();                           /* Assumes static linking of C Runtime */
  67.       WinTerminateOpen32(0);                 /* Tell system we're done using Open32 */
  68.       Dekko32Trace(0xAA80,757,0,NULL);
  69.       return TRUE;
  70.    }
  71.    else
  72.    {
  73.       /* Initialization: A process is gaining access to this DLL */
  74.  
  75.       if ( _CRT_init() == -1 )               /* Initialize C Runtime */
  76.       {
  77.          Dekko32Trace(0xAA80,757,0,NULL);
  78.          return 0;                           /* If initialization failed return 0 */
  79.       }
  80.  
  81.       WinInitializeOpen32(0);                /* Tell system we're using Open32 */
  82.  
  83.       __ctordtorInit();                      /* Initialization code for C++ Objects */
  84.  
  85.       Dekko32Trace(0xAA80,757,0,NULL);
  86.    #ifdef HAVE_DLLENTRYPOINT
  87.       /* Call Win32 Initialization/Termination function                 */
  88.       /* (NOTE: this assumes the DLL entry point is a function called   */
  89.       /*        DllEntryPoint)                                          */
  90.       return DllEntryPoint((HANDLE) handle, DLL_PROCESS_ATTACH, &reserved) != 0;
  91.    #else
  92.       return TRUE;
  93.    #endif
  94.    }
  95. }
  96.