home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / smarts / cdll / initterm.c < prev    next >
Encoding:
Text File  |  1996-02-21  |  4.9 KB  |  124 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.  
  22. /* _CRT_term is the C run-time environment termination function.            */
  23. void _CRT_term(void);
  24.  
  25. size_t nSize;
  26. int *pArray;
  27.  
  28. /*--------------------------------------------------------------------------*/
  29. /* _DLL_InitTerm is the function that gets called by the operating system   */
  30. /* loader when it loads and frees this DLL for each process that accesses   */
  31. /* this DLL.  However, it only gets called the first time the DLL is loaded */
  32. /* and the last time it is freed for a particular process.  The system      */
  33. /* linkage convention MUST be used because the operating system loader is   */
  34. /* calling this function.                                                   */
  35. /*--------------------------------------------------------------------------*/
  36. unsigned long __stdcall _DLL_InitTerm( unsigned long hModule,
  37.                                        unsigned long ulFlag,
  38.                                        long* dummy )
  39. {
  40.    size_t i;
  41.    int  rc;
  42.    char namebuf[_MAX_PATH];
  43.    unsigned long success=1;
  44.  
  45.    /* If ulFlag is one then the DLL is being loaded so initialization should*/
  46.    /* be performed.  If ulFlag is zero then the DLL is being freed so       */
  47.    /* termination should be performed.                                      */
  48.  
  49.    switch (ulFlag)
  50.    {
  51.       case DLL_PROCESS_ATTACH:                   /* The DLL is being loaded */
  52.  
  53.          /*******************************************************************/
  54.          /* The C run-time environment initialization function must be      */
  55.          /* called before any calls to C run-time functions that are not    */
  56.          /* inlined.                                                        */
  57.          /*******************************************************************/
  58.  
  59.          if (_CRT_init() == -1)
  60.          {
  61.             success=0;                                            /* ERROR! */
  62.             break;
  63.          }
  64.  
  65.  
  66.          /*******************************************************************/
  67.          /* For illustrative purposes only, print out the name of the DLL.  */
  68.          /*******************************************************************/
  69.          rc = GetModuleFileName((void *)hModule, namebuf, sizeof(namebuf));
  70.          if (rc == 0)
  71.             printf("GetModuleFileName returned %lu\n", GetLastError());
  72.          else
  73.             printf("The name of this DLL is %s\n", namebuf);
  74.  
  75.          /*******************************************************************/
  76.          /* For illustrative purposes only, a randomly sized array will now */
  77.          /* be created.  It will be freed upon DLL termination.             */
  78.          /*******************************************************************/
  79.          srand(17);
  80.          nSize = (rand()%128)+32;
  81.          printf("The array size for this process is %u\n", nSize);
  82.  
  83.          pArray = malloc(nSize *sizeof(int));
  84.          if( !pArray )
  85.          {
  86.             printf("Could not allocate space for unsorted array.\n");
  87.             success=0;                                            /* ERROR! */
  88.             break;
  89.          }
  90.  
  91.          for (i = 0; i < nSize; ++i )
  92.          {
  93.             pArray[i] = rand();
  94.          }
  95.          break;
  96.  
  97.       case DLL_PROCESS_DETACH:                    /* The DLL is being freed */
  98.          /*******************************************************************/
  99.          /* The array that was allocated during the DLL initialization will */
  100.          /* now be freed.                                                   */
  101.          /*******************************************************************/
  102.  
  103.          printf("The array will now be freed.\n");
  104.          free(pArray);
  105.  
  106.          /**************************************/
  107.          /* terminate our use of the C runtime */
  108.          /**************************************/
  109.          _CRT_term();
  110.  
  111.          break;
  112.  
  113.       default  :
  114.          printf("ulFlag = %lu\n", ulFlag);
  115.          success=0;                                               /* ERROR! */
  116.          break;
  117.    }
  118.  
  119.    /* A non-zero value must be returned to indicate success.                */
  120.  
  121.    return success;
  122. }
  123.  
  124.