home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv7.zip / VACPP / IBMCPP / smarts / CPPDLL / INITTERM.C < prev    next >
Text File  |  1995-06-02  |  6KB  |  145 lines

  1.  
  2. %PROLOG%
  3.  
  4. /*-------------------------------------------------------------*/
  5. /* INITERM.C -- Source for a custom dynamic link library       */
  6. /*              initialization and termination (_DLL_InitTerm) */
  7. /*              function.                                      */
  8. /*                                                             */
  9. /* When called to perform initialization, this sample function */
  10. /* gets storage for an array of integers, and initializes its  */
  11. /* elements with random integers.  At termination time, it     */
  12. /* frees the array.  Substitute your own special processing.   */
  13. /*-------------------------------------------------------------*/
  14.  
  15.  
  16. /* Include files */
  17. #define  INCL_DOSMODULEMGR
  18. #define  INCL_DOSPROCESS
  19. #include <os2.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24.  
  25. /*-------------------------------------------------------------------*/
  26. /* _CRT_init is the C run-time environment initialization function.  */
  27. /* It will return 0 to indicate success and -1 to indicate failure.  */
  28. /*-------------------------------------------------------------------*/
  29. int _CRT_init(void);
  30. #ifdef   STATIC_LINK
  31.  
  32.  
  33. /*-------------------------------------------------------------------*/
  34. /* _CRT_term is the C run-time environment termination function.     */
  35. /* It only needs to be called when the C run-time functions are      */
  36. /* statically linked.                                                */
  37. /*-------------------------------------------------------------------*/
  38. void _CRT_term(void);
  39. #else
  40.  
  41.  
  42. /*-------------------------------------------------------------------*/
  43. /* A clean up routine registered with DosExitList must be used if    */
  44. /* runtime calls are required and the runtime is dynamically linked. */
  45. /* This will guarantee that this clean up routine is run before the  */
  46. /* library DLL is terminated.                                        */
  47. /*-------------------------------------------------------------------*/
  48. static void _System cleanup(ULONG ulReason);
  49. #endif
  50.  
  51.  
  52. /*-------------------------------------------------------------------*/
  53. /* Declare some global variables for the array                       */
  54. /*-------------------------------------------------------------------*/
  55. size_t nSize;
  56. int *pArray;
  57.  
  58.  
  59. /****************************************************************************/
  60. /* _DLL_InitTerm is the function that gets called by the operating system   */
  61. /* loader when it loads and frees this DLL for each process that accesses   */
  62. /* this DLL.  However, it only gets called the first time the DLL is loaded */
  63. /* and the last time it is freed for a particular process.  The system      */
  64. /* linkage convention MUST be used because the operating system loader is   */
  65. /* calling this function.                                                   */
  66. /****************************************************************************/
  67. unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long
  68.                                     ulFlag)
  69. {
  70.    size_t i;
  71.    APIRET rc;
  72.    char namebuf[CCHMAXPATH];
  73.  
  74.    /*-------------------------------------------------------------------------*/
  75.    /* If ulFlag is zero then the DLL is being loaded so initialization should */
  76.    /* be performed.  If ulFlag is 1 then the DLL is being freed so            */
  77.    /* termination should be performed.                                        */
  78.    /*-------------------------------------------------------------------------*/
  79.  
  80.    switch (ulFlag) {
  81.       case 0 :
  82.  
  83.          /*******************************************************************/
  84.          /* The C run-time environment initialization function must be      */
  85.          /* called before any calls to C run-time functions that are not    */
  86.          /* inlined.                                                        */
  87.          /*******************************************************************/
  88.  
  89.          if (_CRT_init() == -1)
  90.             return 0UL;
  91. #ifndef  STATIC_LINK
  92.  
  93.          /*******************************************************************/
  94.          /* A DosExitList routine must be used to clean up if runtime calls */
  95.          /* are required and the runtime is dynamically linked.             */
  96.          /*******************************************************************/
  97.  
  98.             if (rc = DosExitList(0x0000FF00|EXLST_ADD, cleanup))
  99.             printf("DosExitList returned %lu\n", rc);
  100. #endif
  101.          if (rc = DosQueryModuleName(hModule, CCHMAXPATH, namebuf))
  102.             printf("DosQueryModuleName returned %lu\n", rc);
  103.          else
  104.             printf("The name of this DLL is %s\n", namebuf);
  105.          srand(17);
  106.          nSize = (rand()%128)+32;
  107.          printf("The array size for this process is %u\n", nSize);
  108.          if ((pArray = malloc(nSize *sizeof(int))) == NULL) {
  109.             printf("Could not allocate space for unsorted array.\n");
  110.             return 0UL;
  111.          }
  112.          for (i = 0; i < nSize; ++i)
  113.             pArray[i] = rand();
  114.          break;
  115.       case 1 :
  116. #ifdef   STATIC_LINK
  117.          printf("The array will now be freed.\n");
  118.          free(pArray);
  119.          _CRT_term();
  120. #endif
  121.          break;
  122.       default  :
  123.          printf("ulFlag = %lu\n", ulFlag);
  124.          return 0UL;
  125.    }
  126.  
  127.    /***********************************************************/
  128.    /* A non-zero value must be returned to indicate success.  */
  129.    /***********************************************************/
  130.    return 1UL;
  131. }
  132.  
  133.  
  134. #ifndef  STATIC_LINK
  135. static void cleanup(ULONG ulReason)
  136. {
  137.    if (!ulReason) {
  138.       printf("The array will now be freed.\n");
  139.       free(pArray);
  140.    }
  141.    DosExitList(EXLST_EXIT, cleanup);
  142.    return ;
  143. }
  144. #endif
  145.