home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / samples / sam03l / initterm.c__ / INITTERM.C
Encoding:
C/C++ Source or Header  |  1992-04-01  |  5.3 KB  |  143 lines

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