home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mmpm21tk.zip / TK / ADMCT / ADMCINIT.C < prev    next >
C/C++ Source or Header  |  1993-04-13  |  11KB  |  378 lines

  1. /********************* START OF SPECIFICATIONS *********************
  2. *
  3. * SUBROUTINE NAME: ADMCINIT.c
  4. *
  5. *              Copyright (c) IBM Corporation  1991, 1993
  6. *                        All Rights Reserved
  7. *
  8. * DESCRIPTIVE NAME: Audio MCD DLL Intialization and termination
  9. *                  functions.
  10. *
  11.  
  12. * FUNCTION: Create Global heap, carry out per process intialization.
  13. *
  14. * NOTES:
  15. *
  16. * ENTRY POINTS:
  17. *
  18. * INPUT:
  19. *
  20. * EXIT-NORMAL: Return Code 0.
  21. *
  22. * EXIT_ERROR:  Error Code.
  23. *
  24. * EFFECTS:
  25. *
  26. * Global Vars Referenced:
  27. *              hmtxProcSem, heap, UserCount,
  28. *              hEventInitCompleted,
  29. *
  30. * INTERNAL REFERENCES:   ADMCEXIT () -- DLL Termination Routine.
  31. *                        InitADMC () -- DLL Intialization Routine
  32. *
  33. * EXTERNAL REFERENCES:   DosResetEventSem ()        - OS/2 API
  34. *                        DosPostEventSem  ()        - OS/2 API
  35. *                        DosCreateMutexSem()        - OS/2 API
  36. *                        HhpCreateHeap    ()        - MME  API
  37. *                        HhpDestroyHeap   ()        - MME  API
  38. *                        HhpGetPID()      ()        - MME  API
  39. *
  40. *********************** END OF SPECIFICATIONS **********************/
  41.  
  42. #define INCL_BASE
  43. #define INCL_DOSSEMAPHORES
  44. #define INCL_DOSPROCESS
  45. #define INCL_ERRORS
  46.  
  47. #define MCIERR_SUCCESS          0
  48. #define INDEFNITE_PERIOD       -1
  49. #define INTIALIZE               0
  50. #define TERMINATE               1
  51. #define HEAP_SIZE               8192
  52. #define FAILURE                 0L
  53. #define SUCCESS                 1L
  54.  
  55. #include <os2.h>                        // OS/2 System Include
  56. #include <hhpheap.h>                    // Heap Manager Definitions
  57. #include <admcres.h>
  58.  
  59. //MRESULT EXPENTRY ClipboardProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  60.  
  61. /********************* START OF SPECIFICATIONS *******************************
  62. *
  63. * SUBROUTINE NAME: ADMEXIT
  64. *
  65. * DESCRIPTIVE NAME: AudioMCD DLL Temination Routine
  66. *
  67. * FUNCTION: Cleanup and deallocate resources used.
  68. *
  69. *
  70. * ENTRY POINTS:
  71. *
  72. * NOTES: The following concepts are illustrated in this file:
  73. *  A. How to keep track of the number of copies of a dll which
  74. *     are loaded in this system.
  75. *  B. How to allocate and deallocate heaps.
  76. *  C. How to use semaphores accross processes.
  77. *
  78. * INPUT:
  79. *
  80. * EXIT-NORMAL: Return Code 1 to the System Loader.
  81. *
  82. * EXIT_ERROR:  Failure To Load This Module.
  83. *
  84. * EFFECTS:
  85. *
  86. * INTERNAL REFERENCES: heap management Routines.
  87. *
  88. *               Global Variables referenced:
  89. *                      heap.
  90. *                      hmtxProcSem.
  91. *                      UserCount.
  92. *
  93. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  94. *                      DosOpenMutexSem    ()    -  OS/2 API
  95. *                      DosReleaseMutexSem ()    -  OS/2 API
  96. *                      DosCloseMutexSem   ()    -  OS/2 API
  97. *                      DosExitList        ()    -  OS/2 API -- (DCR ed )
  98. *
  99. *********************** END OF SPECIFICATIONS ********************************/
  100.  
  101. USHORT ADMCEXIT ()                    /* AudioMCD Exit Routine    */
  102. {
  103.  
  104.   extern int          UserCount;   // Number of Dynamic links
  105.   extern HHUGEHEAP    heap;        // Global Heap
  106.   extern HMTX         hmtxProcSem; // Global Mutex semaphore
  107.   ULONG               ulrc;        // Return Code
  108.  
  109.   ulrc = MCIERR_SUCCESS;
  110.  
  111.   /*************************************************
  112.   * Since we are terminating, there is one less
  113.   * copy of our dll alive, so reduce the global
  114.   * memory count of the number of dll's alive by
  115.   * one.  This count is important, since if we ever
  116.   * are reduced to 0 copies of the dll, we should
  117.   * free the heap memory that we allocatd.
  118.   *************************************************/
  119.  
  120.   UserCount--;
  121.  
  122.  
  123.  
  124.   /*****************************************
  125.   * Request the mutex semaphore.
  126.   ******************************************/
  127.   if ((ulrc = DosRequestMutexSem ( hmtxProcSem,
  128.                                    INDEFNITE_PERIOD)) != 0)
  129.     {
  130.     DosCloseMutexSem (hmtxProcSem);
  131.     return (FAILURE);
  132.     }
  133.  
  134.  
  135.   /*********************************************
  136.   * If there are no more dll's alive, destroy the
  137.   * heap.
  138.   **********************************************/
  139.  
  140.   if (UserCount == 0)
  141.      {
  142.      HhpDestroyHeap (heap);    /* Destroy the heap */
  143.      }
  144.  
  145.   /*********************************************
  146.   * If not the last dll, then release access to heap
  147.   **********************************************/
  148.  
  149.   if (UserCount > 0)
  150.     {
  151.     HhpReleaseHeap (heap, HhpGetPID());  /* Release access */
  152.     }
  153.  
  154.   DosReleaseMutexSem (hmtxProcSem);
  155.   DosCloseMutexSem (hmtxProcSem);
  156.  
  157.   return (SUCCESS);
  158. } /* ADMCExit */
  159.  
  160.  
  161.  
  162.  
  163. /********************* START OF SPECIFICATIONS *******************************
  164. *
  165. * SUBROUTINE NAME: ADMCINIT.C
  166. *
  167. * DESCRIPTIVE NAME: AudioMCD DLL Ibtialization.
  168. *
  169. * FUNCTION: Intialize the DLL and provide mem access to multiple processes.
  170. *
  171. * NOTES: Access to The Global heap is provided using the heap manager
  172. *        Routines. The DLL_InitTerm function is the first function
  173. *        called by the OS/2 DLL Loader.  If the flag variable is set to
  174. *        0, then this is DLL initialization, else it will be termination.
  175. *
  176. *        With the global variable ulUserCount, we keep track of how many
  177. *        times this dll is loaded (i.e. how many process have loaded the
  178. *        DLL).  This variable is used to monitor access to the global
  179. *        memory heap that all instances of the dll maintain.
  180. *
  181. *
  182. * ENTRY POINTS:
  183. *
  184. * INPUT:
  185. *
  186. * EXIT-NORMAL: Return Code 1 to the System Loader.
  187. *
  188. * EXIT_ERROR:  Failure To Load This Module.
  189. *
  190. * EFFECTS:
  191. *
  192. * INTERNAL REFERENCES: heap management Routines,
  193. *
  194. *               Global Variables referenced:
  195. *                      heap.
  196. *                      hmtxProcSem.
  197. *                      UserCount.
  198. *
  199. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  200. *                      DosOpenMutexSem    ()    -  OS/2 API
  201. *                      DosReleaseMutexSem ()    -  OS/2 API
  202. *                      DosCloseMutexSem   ()    -  OS/2 API
  203. *                      DosExitList        ()    -  OS/2 API
  204. *
  205. *********************** END OF SPECIFICATIONS ********************************/
  206.  
  207. /* COVCC_!INSTR */
  208. #pragma linkage (_DLL_InitTerm, system)
  209.  
  210. /* COVCC_!INSTR */
  211. unsigned long _DLL_InitTerm (unsigned long hModhandle, unsigned long Flag)
  212.  
  213.  
  214. {
  215.  
  216.   extern int          UserCount;
  217.   extern HHUGEHEAP    heap;
  218.   extern HMTX         hmtxProcSem;
  219.  
  220.   extern ULONG        hModuleHandle;
  221.  
  222. //  extern   HAB        hab;
  223. //  extern   HMQ        hmq;
  224. //  extern   QMSG       qmsg;
  225. //  extern   HWND       hwndClipWin;               /* Clipboard win handle */
  226.  
  227.  
  228.  
  229.   ULONG               ulrc;
  230.   USHORT              RCExit;
  231.   ulrc = hModhandle;      // Subdue the Warning for now
  232.   ulrc = MCIERR_SUCCESS;
  233.  
  234.   /*********************************************
  235.   * Store the module handle for future reference
  236.   * (i.e. GpiLoadBitmap)
  237.   **********************************************/
  238.  
  239.  
  240.   hModuleHandle = hModhandle;
  241.  
  242.   if (Flag == INTIALIZE )
  243.        {
  244.  
  245.        /**************************************
  246.        * Create a semaphore which will be used
  247.        * by all instances of this dll to prevent
  248.        * one copy of the dll from overwriting
  249.        * another's memory etc.
  250.        ***************************************/
  251.  
  252.        if ( UserCount == 0 )
  253.           {
  254.             ulrc = DosCreateMutexSem ( (ULONG)NULL,
  255.                                        &hmtxProcSem,
  256.                                        DC_SEM_SHARED,
  257.                                        FALSE );
  258.             if (ulrc)
  259.                {
  260.                return (FAILURE);  /* Failed Sem Create */
  261.                }
  262.  
  263.           } /* if this is the first instance of the dll */
  264.  
  265.        _CRT_init();
  266.  
  267.        /***************************************
  268.        * Open the Mutex Semaphore for process control
  269.        ****************************************/
  270.  
  271.        ulrc = DosOpenMutexSem ((ULONG)NULL, &hmtxProcSem);
  272.  
  273.        if (ulrc)
  274.           {
  275.           return (FAILURE);
  276.           }
  277.  
  278.        /****************************************
  279.        * Acquire The Mutex Semaphore
  280.        *****************************************/
  281.  
  282.        if ((ulrc = DosRequestMutexSem ( hmtxProcSem,
  283.                                         INDEFNITE_PERIOD)) != 0)
  284.          {
  285.  
  286.          DosCloseMutexSem (hmtxProcSem);
  287.          return (FAILURE);
  288.          }
  289.  
  290.        /**************************************
  291.        * If we are the first copy of the dll
  292.        * currently loaded, then create a heap
  293.        * where all of our memory will be
  294.        * allocated from.
  295.        ***************************************/
  296.  
  297.        if (UserCount == 0)
  298.           {
  299.  
  300.            heap = HhpCreateHeap (HEAP_SIZE, HH_SHARED);
  301.  
  302.            if (heap == (ULONG)NULL)
  303.               {
  304.               DosReleaseMutexSem (hmtxProcSem);
  305.               DosCloseMutexSem (hmtxProcSem);
  306.               return (FAILURE);
  307.               }
  308.  
  309.           }
  310.  
  311.        /*************************************************
  312.        * If the user count is greater than one
  313.        * then we just provide access to the global heap
  314.        * which was created on the load of the initial
  315.        * copy of this dll.
  316.        **************************************************/
  317.  
  318.        if (UserCount != 0)
  319.           {
  320.           /**********************************
  321.           * Provide Shared Access to heap
  322.           ***********************************/
  323.           ulrc = HhpAccessHeap (heap, HhpGetPID());
  324.           if (ulrc)
  325.                return (FAILURE);
  326.           }
  327.  
  328.        /******************************************
  329.        * Release The Mutex Semaphore
  330.        *******************************************/
  331.  
  332.        if ((ulrc = DosReleaseMutexSem (hmtxProcSem)) != 0L)
  333.           {
  334.           if (UserCount != 0)
  335.                 DosCloseMutexSem (hmtxProcSem);
  336.  
  337.           return (FAILURE);
  338.           }
  339.  
  340.  
  341.        /*****************************************
  342.        * The stream handlers, but only for the
  343.        * first DLL
  344.        *****************************************/
  345.  
  346.        ulrc = StreamSetup ( );
  347.  
  348.        if ( ulrc )
  349.           {
  350.           return ( ulrc );
  351.           }
  352.  
  353.        UserCount++;                         /* Increment Usage */
  354.  
  355.  
  356.        /***********************************************
  357.        * Init Complete, Indicate success to the loader
  358.        ************************************************/
  359.  
  360.        return (SUCCESS);
  361.  
  362.        } /* if this is DLL initialization */
  363.  
  364.     /********************************************
  365.     * If the flag is anything but initialization
  366.     * then it MUST be termination.
  367.     *********************************************/
  368.  
  369.     else
  370.        {
  371.           RCExit = ADMCEXIT ();
  372.           return (RCExit);
  373.        } /* else this must be termination */
  374.  
  375. } /* of Init Term Function */
  376.  
  377. /* COVCC_INSTR */
  378.