home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / mm / admct / admcinit.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  11KB  |  358 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 INTIALIZE               0
  49. #define TERMINATE               1
  50. #define FAILURE                 0L
  51. #define SUCCESS                 1L
  52. #define ADMC_SEM_NAME           "\\SEM32\\ADMC"
  53.  
  54. #include <os2.h>                        // OS/2 System Include
  55. #include <string.h>
  56. #include <os2medef.h>                   // MME includes files.
  57. #include <stdlib.h>                     // Math functions
  58. #include <ssm.h>                        // SSM Spi includes.
  59. #include <meerror.h>                    // MM Error Messages.
  60. #include <mmioos2.h>                    // MMIO Include.
  61. #include <mcios2.h>                     // MM System Include.
  62. #include <mcipriv.h>                    // MCI Connection stuff
  63. #include <mmdrvos2.h>                   // MCI Driver include.
  64. #include <mcd.h>                        // AudioIFDriverInterface.
  65. #include <hhpheap.h>                    // Heap Manager Definitions
  66. #include <qos.h>
  67. #include "audiomcd.h"                   // Component Definitions.
  68. #include "admcfunc.h"                   // Function Prototypes.
  69. #include "admcres.h"
  70. #include <mmdsload.h>
  71.  
  72. int _CRT_init( VOID );
  73.  
  74. //MRESULT EXPENTRY ClipboardProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  75. extern int          UserCount;
  76. extern HHUGEHEAP    heap;
  77. extern HMTX         hmtxProcSem;
  78. extern ULONG        hModuleHandle;
  79.  
  80. //MRESULT EXPENTRY ClipboardProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 );
  81.  
  82. /********************* START OF SPECIFICATIONS *******************************
  83. *
  84. * SUBROUTINE NAME: ADMEXIT
  85. *
  86. * DESCRIPTIVE NAME: AudioMCD DLL Temination Routine
  87. *
  88. * FUNCTION: Cleanup and deallocate resources used.
  89. *
  90. *
  91. * ENTRY POINTS:
  92. *
  93. * NOTES: The following concepts are illustrated in this file:
  94. *  A. How to keep track of the number of copies of a dll which
  95. *     are loaded in this system.
  96. *  B. How to allocate and deallocate heaps.
  97. *  C. How to use semaphores accross processes.
  98. *
  99. * INPUT:
  100. *
  101. * EXIT-NORMAL: Return Code 1 to the System Loader.
  102. *
  103. * EXIT_ERROR:  Failure To Load This Module.
  104. *
  105. * EFFECTS:
  106. *
  107. * INTERNAL REFERENCES: heap management Routines.
  108. *
  109. *               Global Variables referenced:
  110. *                      heap.
  111. *                      hmtxProcSem.
  112. *                      UserCount.
  113. *
  114. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  115. *                      DosOpenMutexSem    ()    -  OS/2 API
  116. *                      DosReleaseMutexSem ()    -  OS/2 API
  117. *                      DosCloseMutexSem   ()    -  OS/2 API
  118. *                      DosExitList        ()    -  OS/2 API -- (DCR ed )
  119. *
  120. *********************** END OF SPECIFICATIONS ********************************/
  121.  
  122.  
  123. USHORT ADMCEXIT ()                    /* AudioMCD Exit Routine    */
  124. {
  125.  
  126.   ULONG               ulrc;        // Return Code
  127.  
  128.   ulrc = MCIERR_SUCCESS;
  129.  
  130.   /*************************************************
  131.   * Since we are terminating, there is one less
  132.   * copy of our dll alive, so reduce the global
  133.   * memory count of the number of dll's alive by
  134.   * one.  This count is important, since if we ever
  135.   * are reduced to 0 copies of the dll, we should
  136.   * free the heap memory that we allocatd.
  137.   *************************************************/
  138.  
  139.  
  140.  
  141.  
  142.   /*****************************************
  143.   * Request the mutex semaphore.
  144.   ******************************************/
  145.   if ((ulrc = DosRequestMutexSem ( hmtxProcSem, SEM_INDEFINITE_WAIT)) != 0)
  146.     {
  147.     DosCloseMutexSem (hmtxProcSem);
  148.     return (FAILURE);
  149.     }
  150.  
  151.   UserCount--;
  152.  
  153.   /*********************************************
  154.   * If there are no more dll's alive, destroy the
  155.   * heap.
  156.   **********************************************/
  157.   if (!UserCount)
  158.   {
  159.      HhpDestroyHeap (heap);    /* Destroy the heap */
  160.      heap = 0;
  161.   }
  162.   else
  163.   {
  164.     /*********************************************
  165.     * If not the last dll, then release access to heap
  166.     **********************************************/
  167.     HhpReleaseHeap (heap, HhpGetPID());  /* Release access */
  168.   }
  169.  
  170.   DosReleaseMutexSem (hmtxProcSem);
  171.   DosCloseMutexSem (hmtxProcSem);
  172.   return (SUCCESS);
  173. } /* ADMCExit */
  174.  
  175.  
  176. /********************* START OF SPECIFICATIONS *******************************
  177. *
  178. * SUBROUTINE NAME: ADMCINIT.C
  179. *
  180. * DESCRIPTIVE NAME: AudioMCD DLL Ibtialization.
  181. *
  182. * FUNCTION: Intialize the DLL and provide mem access to multiple processes.
  183. *
  184. * NOTES: Access to The Global heap is provided using the heap manager
  185. *        Routines. The DLL_InitTerm function is the first function
  186. *        called by the OS/2 DLL Loader.  If the flag variable is set to
  187. *        0, then this is DLL initialization, else it will be termination.
  188. *
  189. *        With the global variable ulUserCount, we keep track of how many
  190. *        times this dll is loaded (i.e. how many process have loaded the
  191. *        DLL).  This variable is used to monitor access to the global
  192. *        memory heap that all instances of the dll maintain.
  193. *
  194. *
  195. * ENTRY POINTS:
  196. *
  197. * INPUT:
  198. *
  199. * EXIT-NORMAL: Return Code 1 to the System Loader.
  200. *
  201. * EXIT_ERROR:  Failure To Load This Module.
  202. *
  203. * EFFECTS:
  204. *
  205. * INTERNAL REFERENCES: heap management Routines,
  206. *
  207. *               Global Variables referenced:
  208. *                      heap.
  209. *                      hmtxProcSem.
  210. *                      UserCount.
  211. *
  212. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  213. *                      DosOpenMutexSem    ()    -  OS/2 API
  214. *                      DosReleaseMutexSem ()    -  OS/2 API
  215. *                      DosCloseMutexSem   ()    -  OS/2 API
  216. *                      DosExitList        ()    -  OS/2 API
  217. *
  218. *********************** END OF SPECIFICATIONS ********************************/
  219.  
  220. /* COVCC_!INSTR */
  221. #pragma linkage (_DLL_InitTerm, system)
  222.  
  223. /* COVCC_!INSTR */
  224. unsigned long APIENTRY _ADMCInitTerm (unsigned long hModhandle, unsigned long Flag );
  225. unsigned long APIENTRY _ADMCInitTerm (unsigned long hModhandle, unsigned long Flag )
  226.  
  227.  
  228. {
  229.  
  230.  
  231. //  extern   HAB        hab;
  232. //  extern   HMQ        hmq;
  233. //  extern   QMSG       qmsg;
  234. //  extern   HWND       hwndClipWin;               /* Clipboard win handle */
  235.  
  236.  
  237.  
  238.   ULONG               ulrc;
  239.   USHORT              RCExit;
  240.   ULONG               hmodSSM;
  241.   ULONG               hmodMMIO;
  242.   ulrc = hModhandle;      // Subdue the Warning for now
  243.   ulrc = MCIERR_SUCCESS;
  244.  
  245.   /*********************************************
  246.   * Store the module handle for future reference
  247.   * (i.e. GpiLoadBitmap)
  248.   **********************************************/
  249.  
  250.  
  251.   hModuleHandle = hModhandle;
  252.  
  253.   if (Flag == INTIALIZE )
  254.   {
  255.        /**************************************
  256.        * Create a semaphore which will be used
  257.        * by all instances of this dll to prevent
  258.        * one copy of the dll from overwriting
  259.        * another's memory etc.
  260.        ***************************************/
  261.        _CRT_init();
  262.        ulrc = DosCreateMutexSem (ADMC_SEM_NAME, &hmtxProcSem, 0L, FALSE);
  263.        if (ulrc == ERROR_DUPLICATE_NAME)
  264.        {
  265.            ulrc = DosOpenMutexSem (ADMC_SEM_NAME, &hmtxProcSem);
  266.        }
  267.  
  268.        if (ulrc)
  269.           return (FAILURE);  /* Failed Sem Create */
  270.  
  271.        /****************************************
  272.        * Acquire The Mutex Semaphore
  273.        *****************************************/
  274.        ulrc = DosRequestMutexSem ( hmtxProcSem, SEM_INDEFINITE_WAIT);
  275.        if (ulrc)
  276.        {
  277.          DosCloseMutexSem (hmtxProcSem);
  278.          return (FAILURE);
  279.        }
  280.  
  281.  
  282.        /**************************************
  283.        * If we are the first copy of the dll
  284.        * currently loaded, then create a heap
  285.        * where all of our memory will be
  286.        * allocated from.
  287.        ***************************************/
  288.        if (!UserCount && !heap)
  289.        {
  290.            heap = HhpCreateHeap (HEAP_SIZE, HH_SHARED);
  291.            if (heap == (ULONG)NULL)
  292.            {
  293.               DosReleaseMutexSem (hmtxProcSem);
  294.               DosCloseMutexSem (hmtxProcSem);
  295.               return (FAILURE);
  296.            }
  297.  
  298.  
  299.        }
  300.        else
  301.        {
  302.          /*************************************************
  303.          * If the user count is greater than one
  304.          * then we just provide access to the global heap
  305.          * which was created on the load of the initial
  306.          * copy of this dll.
  307.          **************************************************/
  308.           ulrc = HhpAccessHeap (heap, HhpGetPID());
  309.           if (ulrc)
  310.                return (FAILURE);
  311.        }
  312.  
  313.        UserCount++;                         /* Increment Usage */
  314.  
  315.        /*****************************************
  316.        * The stream handlers, but only for the
  317.        * first DLL
  318.        *****************************************/
  319.        MM_DosLoadModule (NULL, 0L, "SSM",  &hmodSSM);
  320.        MM_DosLoadModule (NULL, 0L, "MMIO", &hmodMMIO);
  321.        ulrc = StreamSetup ( );
  322.        if ( ulrc )
  323.        {
  324.           DosReleaseMutexSem (hmtxProcSem);
  325.           return ( ulrc );
  326.        }
  327.  
  328.        /******************************************
  329.        * Release The Mutex Semaphore
  330.        *******************************************/
  331.        ulrc = DosReleaseMutexSem (hmtxProcSem);
  332.        if (ulrc)
  333.        {
  334.           if (UserCount)
  335.             DosCloseMutexSem (hmtxProcSem);
  336.           return (FAILURE);
  337.        }
  338.  
  339.        /***********************************************
  340.        * Init Complete, Indicate success to the loader
  341.        ************************************************/
  342.        return (SUCCESS);
  343.  
  344.   } /* if this is DLL initialization */
  345.   else
  346.   {
  347.       /********************************************
  348.       * If the flag is anything but initialization
  349.       * then it MUST be termination.
  350.       *********************************************/
  351.       RCExit = ADMCEXIT ();
  352.       return (RCExit);
  353.   } /* else this must be termination */
  354.  
  355. } /* of Init Term Function */
  356.  
  357. /* COVCC_INSTR */
  358.