home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / compiler / sample03 / sort / initterm.c next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  4.3 KB  |  100 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Sample Program 03 : INITTERM.C                                           */
  4. /*                                                                          */
  5. /* COPYRIGHT:                                                               */
  6. /* ----------                                                               */
  7. /* Copyright (C) International Business Machines Corp., 1991, 1993.         */
  8. /*                                                                          */
  9. /* DISCLAIMER OF WARRANTIES:                                                */
  10. /* -------------------------                                                */
  11. /* The following [enclosed] code is sample code created by IBM              */
  12. /* Corporation.  This sample code is not part of any standard IBM product   */
  13. /* and is provided to you solely for the purpose of assisting you in the    */
  14. /* development of your applications.  The code is provided "AS IS",         */
  15. /* without warranty of any kind.  IBM shall not be liable for any damages   */
  16. /* arising out of your use of the sample code, even if they have been       */
  17. /* advised of the possibility of such damages.                              */
  18. /*                                                                          */
  19. /****************************************************************************/
  20.  
  21. #include <windows.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #define  CCHMAXPATH  255
  26.  
  27. /* _CRT_init is the C run-time environment initialization function.         */
  28. /* It will return 0 to indicate success and -1 to indicate failure.         */
  29.  
  30. int _CRT_init(void);
  31.  
  32. /* _CRT_term is the C run-time environment termination function.            */
  33.  
  34. void _CRT_term(void);
  35.  
  36.  
  37. size_t _Export nSize;
  38. int * _Export pArray;
  39.  
  40. /* _DLL_InitTerm is the function that gets called by the operating system   */
  41. /* loader when it loads and frees this DLL for each process that accesses   */
  42. /* this DLL.  However, it only gets called the first time the DLL is loaded */
  43. /* and the last time it is freed for a particular process.  The system      */
  44. /* linkage convention MUST be used because the operating system loader is   */
  45. /* calling this function.                                                   */
  46.  
  47. unsigned long __stdcall _DLL_InitTerm(unsigned long hModule, unsigned long
  48.                                     ulFlag, long * dummy)
  49. {
  50.    size_t i;
  51.    int  rc;
  52.    char namebuf[CCHMAXPATH];
  53.  
  54.    /* If ulFlag is DLL_PROCESS_ATTACH then the DLL is being loaded so initialization should*/
  55.    /* be performed.  If ulFlag is DLL_PROCESS_DETACH then the DLL is being freed so       */
  56.    /* termination should be performed.                                      */
  57.  
  58.    switch (ulFlag) {
  59.       case DLL_PROCESS_ATTACH :
  60.  
  61.          /*******************************************************************/
  62.          /* The C run-time environment initialization function must be      */
  63.          /* called before any calls to C run-time functions that are not    */
  64.          /* inlined.                                                        */
  65.          /*******************************************************************/
  66.  
  67.          if (_CRT_init() == -1)
  68.             return 0UL;
  69.  
  70.          if ((rc = GetModuleFileName((void *)hModule, namebuf, CCHMAXPATH)) == 0)
  71.             printf("GetModuleFileName returned %lu\n", GetLastError());
  72.          else
  73.             printf("The name of this DLL is %s\n", namebuf);
  74.          srand(17);
  75.          nSize = (rand()%128)+32;
  76.          printf("The array size for this process is %u\n", nSize);
  77.          if ((pArray = malloc(nSize *sizeof(int))) == NULL) {
  78.             printf("Could not allocate space for unsorted array.\n");
  79.             return 0UL;
  80.          }
  81.          for (i = 0; i < nSize; ++i)
  82.             pArray[i] = rand();
  83.          break;
  84.  
  85.       case DLL_PROCESS_DETACH :
  86.          printf("The array will now be freed.\n");
  87.          free(pArray);
  88.          _CRT_term();
  89.          break;
  90.  
  91.       default  :
  92.          printf("ulFlag = %lu\n", ulFlag);
  93.          return 0UL;
  94.    }
  95.  
  96.    /* A non-zero value must be returned to indicate success.                */
  97.  
  98.    return 1UL;
  99. }
  100.