Example of a Subsystem _DLL_InitTerm Function (Windows)

/* _DLL_InitTerm() - called by the loader for DLL initialization/termination  */
/* This function must return a non-zero value if successful and a zero value  */
/* if unsuccessful.                                                           */


unsigned long __stdcall _DLL_InitTerm( HINSTANCE hModule,

                                     DWORD ulFlag, LPVOID dummy)

   {

   ULONG rc;



   /* If ulFlag is DLL_PROCESS_ATTACH then initialization is required:        */
   /*    If the shared memory pointer is NULL then the DLL is being loaded    */
   /*    for the first time so acquire the named shared storage for the       */
   /*    process control structures.  A linked list of process control        */
   /*    structures will be maintained.  Each time a new process loads this   */
   /*    DLL, a new process control structure is created and it is inserted   */
   /*    at the end of the list by calling DLLREGISTER.                       */
   /*                                                                         */
   /* If ulFlag is DLL_PROCESS_DETTACH then termination is required:          */
   /*    Call DLLDEREGISTER which will remove the process control structure   */
   /*    and free the shared memory block from its virtual address space.     */


   switch( ulFlag )

      {

      case DLL_PROCESS_ATTACH:

         _rmem_init();

         if ( !ulProcessCount )

            {

            /* Create the shared mutex semaphore.                             */



            if (( hmtxSharedSem = CreateMutex(NULL,

                                              FALSE,

                                              SHARED_SEMAPHORE_NAME)) == NULL)

               {

               printf( "CreateMutex  rc = %lu\n", GetLastError() );

               return FALSE;

               }

            }


         /* Register the current process.                                     */


         if ( DLLREGISTER( ) )

            return FALSE;



         break;



      case DLL_PROCESS_DETACH:

         /* De-register the current process.                                  */



         if ( DLLDEREGISTER( ) )

            return 0;



         _rmem_term();

         break;



      default:

         return 0;

      }


   /* Indicate success.  Non-zero means success!!!                            */


   return TRUE;

   }


WriteYour Own _DLL_InitTerm Function