Example of an OS/2 _DLL_InitTerm Function

Here is an example of a user written_DLL_InitTerm function. It overrides the run-time library function of the same name to initialize and terminate a DLL.

The DLL for this routine performs some activities on randomly generated data, and the initialization of the DLL prepares the data. The initialization done by this _DLL_InitTerm function involves allocating memory to store an array. The size of the array is random, determined by calling the random number function, and the elements of the array contain random numbers. The termination involves deallocating the memory occupied by the array.

#define  INCL_DOSMODULEMGR
#define  INCL_DOSPROCESS
#define  INCL_DOSERRORS
#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* _CRT_init is the C run-time environment initialization function.*/
/* It will return 0 to indicate success and -1 to indicate failure.*/
extern int _IMPORT _CRT_init(void);
/* _CRT_term is the C run-time environment termination function.   */
extern void _IMPORT _CRT_term(void);
size_t _Export nSize;
int *_Export pArray;
/* _DLL_InitTerm is the function that gets called by the operating */
/* system loader when it loads and frees this DLL for each process */
/* that accesses this DLL.  However, it only gets called the first */
/* time the DLL is loaded and the last time it is freed for a      */
/* particular process. The system linkage convention MUST be used  */ 
/* because the operating system loader is calling this function.   */
unsigned long _System _DLL_InitTerm(unsigned long hModule, 
                                    unsigned long ulFlag)
{
   size_t i;
   APIRET rc;
   char namebuf[CCHMAXPATH];
/* If ulFlag is zero then the DLL is being loaded so initialization*/
/* be performed.  If ulFlag is 1 then the DLL is being freed so    */
/* termination should be performed.                                */
   switch (ulFlag) {
      case 0 :
/*******************************************************************/
/* The C run-time environment initialization function must be      */
/* called before any calls to C run-time functions that are not    */
/* inlined.                                                        */
/*******************************************************************/
         if (_CRT_init() == -1)
            return 0UL;
         if ((rc = DosQueryModuleName(hModule, CCHMAXPATH, namebuf)) 
                                                        != NO_ERROR)
            printf("DosQueryModuleName returned %lu\n", rc);
         else
            printf("The name of this DLL is %s\n", namebuf);
         srand(17);
         nSize = (rand()%128)+32;
         printf("The array size for this process is %u\n", nSize);
         if ((pArray = malloc(nSize *sizeof(int))) == NULL) {
            printf("Could not allocate space for unsorted array.\n");
            return 0UL;
         }
         for (i = 0; i < nSize; ++i)
            pArray[i] = rand();
         break;
      case 1 :
         printf("The array will now be freed.\n");
         free(pArray);
         _CRT_term();
         break;
      default  :
         printf("ulFlag = %lu\n", ulFlag);
         return 0UL;
   }
   /* A non-zero value must be returned to indicate success.                */
   return 1UL;
}


Static and Dynamic Link Libraries
Run-Time Library Environments
Subsystems


Build Dynamic Link Libraries
Initialize and Terminate Your DLL environment
Build Subsystems


The _DLL_InitTerm Function