home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / smarts / cppdll / initterm.c < prev    next >
Encoding:
Text File  |  1996-02-21  |  5.3 KB  |  136 lines

  1. --------------------------------------------------------------------------------
  2. --
  3. -- COPYRIGHT:
  4. --   IBM WorkFrame - Project Smarts
  5. --   (C) Copyright International Business Machines Corporation 1996
  6. --   Licensed Material - Program-Property of IBM - All Rights Reserved.
  7. --   US Government Users Restricted Rights - Use, duplication, or disclosure
  8. --   restricted by GSA ADP Schedule Contract with IBM Corp.
  9. --
  10. --------------------------------------------------------------------------------
  11. <include prologc.tde>
  12.  
  13. #include <windows.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. /* _CRT_init is the C run-time environment initialization function.         */
  19. /* It will return 0 to indicate success and -1 to indicate failure.         */
  20. int _CRT_init(void);
  21. void __ctordtorInit(void);
  22.  
  23. /* _CRT_term is the C run-time environment termination function.            */
  24. void __ctordtorTerm(void);
  25. void _CRT_term(void);
  26.  
  27. size_t nSize;
  28. int *pArray;
  29.  
  30. /*--------------------------------------------------------------------------*/
  31. /* _DLL_InitTerm is the function that gets called by the operating system   */
  32. /* loader when it loads and frees this DLL for each process that accesses   */
  33. /* this DLL.  However, it only gets called the first time the DLL is loaded */
  34. /* and the last time it is freed for a particular process.  The system      */
  35. /* linkage convention MUST be used because the operating system loader is   */
  36. /* calling this function.                                                   */
  37. /*--------------------------------------------------------------------------*/
  38. unsigned long __stdcall _DLL_InitTerm( unsigned long hModule,
  39.                                        unsigned long ulFlag,
  40.                                        long* dummy )
  41. {
  42.    size_t i;
  43.    int  rc;
  44.    char namebuf[_MAX_PATH];
  45.    unsigned long success=1;
  46.  
  47.    /* If ulFlag is one then the DLL is being loaded so initialization should*/
  48.    /* be performed.  If ulFlag is zero then the DLL is being freed so       */
  49.    /* termination should be performed.                                      */
  50.  
  51.    switch (ulFlag)
  52.    {
  53.       case DLL_PROCESS_ATTACH:                   /* The DLL is being loaded */
  54.  
  55.          /*******************************************************************/
  56.          /* The C run-time environment initialization function must be      */
  57.          /* called before any calls to C run-time functions that are not    */
  58.          /* inlined.                                                        */
  59.          /*******************************************************************/
  60.  
  61.          if (_CRT_init() == -1)
  62.          {
  63.             success=0;                                            /* ERROR! */
  64.             break;
  65.          }
  66.  
  67.          /**************************************/
  68.          /* initialize any static C++ objects */
  69.          /**************************************/
  70.          __ctordtorInit();
  71.  
  72.  
  73.          /*******************************************************************/
  74.          /* For illustrative purposes only, print out the name of the DLL.  */
  75.          /*******************************************************************/
  76.          rc = GetModuleFileName((void *)hModule, namebuf, sizeof(namebuf));
  77.          if (rc == 0)
  78.             printf("GetModuleFileName returned %lu\n", GetLastError());
  79.          else
  80.             printf("The name of this DLL is %s\n", namebuf);
  81.  
  82.          /*******************************************************************/
  83.          /* For illustrative purposes only, a randomly sized array will now */
  84.          /* be created.  It will be freed upon DLL termination.             */
  85.          /*******************************************************************/
  86.          srand(17);
  87.          nSize = (rand()%128)+32;
  88.          printf("The array size for this process is %u\n", nSize);
  89.  
  90.          pArray = malloc(nSize *sizeof(int));
  91.          if( !pArray )
  92.          {
  93.             printf("Could not allocate space for unsorted array.\n");
  94.             success=0;                                            /* ERROR! */
  95.             break;
  96.          }
  97.  
  98.          for (i = 0; i < nSize; ++i )
  99.          {
  100.             pArray[i] = rand();
  101.          }
  102.          break;
  103.  
  104.       case DLL_PROCESS_DETACH:                    /* The DLL is being freed */
  105.          /*******************************************************************/
  106.          /* The array that was allocated during the DLL initialization will */
  107.          /* now be freed.                                                   */
  108.          /*******************************************************************/
  109.  
  110.          printf("The array will now be freed.\n");
  111.          free(pArray);
  112.  
  113.          /**************************************/
  114.          /* destruct any static C++ objects    */
  115.          /**************************************/
  116.          __ctordtorTerm();
  117.  
  118.          /**************************************/
  119.          /* terminate our use of the C runtime */
  120.          /**************************************/
  121.          _CRT_term();
  122.  
  123.          break;
  124.  
  125.       default  :
  126.          printf("ulFlag = %lu\n", ulFlag);
  127.          success=0;                                               /* ERROR! */
  128.          break;
  129.    }
  130.  
  131.    /* A non-zero value must be returned to indicate success.                */
  132.  
  133.    return success;
  134. }
  135.  
  136.