home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / ThreadRunn60422382002.psc / BGTRDemo / Auxiliary DLLs / SharedMemMaps32 / Classes / CMemoryMapFile.cls
Encoding:
Visual Basic class definition  |  2002-03-03  |  5.3 KB  |  137 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CMemoryMapFile"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. '********************************************************************************************
  17. '   CMemoryMapFile Class Definition
  18. '
  19. '   This class encapsulates the entire process of creating shared memory,
  20. '   creating synchronization objects, and reading and writing to the memory.
  21. '
  22. '   A class instance represents a single shared memory file mapping.
  23. '
  24. '   Instancing is set to:  1 - Private Use
  25. '********************************************************************************************
  26. Option Explicit
  27.  
  28. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  29. '^  Magic numbers for file mapping API calls
  30. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. 'Declare a constant of 0 for the security attributes
  32. Private Const SEC_ATTRIBUTES As Long = 0
  33.  
  34. 'Declare a constant of -1 for the hFile handle because these functions
  35. 'will read/write blocks of memory directly, with no use of a file.
  36. Private Const HFILE_NONE As Long = -1
  37. 'Declare a constant of 0 for the memory File maximum size highword
  38. Private Const MAP_SIZE_HIGH As Long = 0
  39. 'Declare constants for memory offsets - none are used
  40. Private Const OFFSET_HIGH As Long = 0
  41. Private Const OFFSET_LOW As Long = 0
  42. ''Declare constants for memory access - all of it
  43. Private Const ACCESS_ALL As Long = 0
  44.  
  45. 'Declare const for mutex initial ownership
  46. Private Const OWN_MUTEX_NO As Boolean = False
  47. 'Declare const for maximum wait time for a mutex - msecs
  48. Private Const WAIT_MAX_MUTEX As Long = 5000
  49.  
  50. 'Declare const for semaphore access priviliges - full access
  51. Private Const SEM_ACCESS_FULL As Long = -1
  52. 'Declare const for semaphore inheritence - cannot be inherited
  53. Private Const CANT_INHERIT As Boolean = False
  54. 'Declare const for maximum wait time for a semaphore - msecs
  55. Private Const WAIT_MAX_SEM As Long = 1000
  56.  
  57. 'Declare const for sleep time if required - msecs
  58. Private Const NAP_TIME As Long = 100
  59.  
  60. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  61. '^ Declares for various objects and handles
  62. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  63. 'Memory File handles
  64. Private m_hwndMemoryMap As Long
  65. Private m_pMemoryMap As Long
  66. '
  67. 'Synchronization object handles
  68. Private m_hwndMutex As Long
  69. Private m_hwndSemaphore As Long
  70.  
  71. 'Maximum number of semaphores to create
  72. 'also represents the maximum threads that can
  73. 'use a given shared memory File.
  74. Private All_Semaphores As Long
  75.  
  76. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. '^ Memory map file creation function
  78. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  79.  
  80. Public Sub MapMemory(strShareName As String, lngMaxLength As Long)
  81. On Error GoTo CatchErr
  82. Dim strErrorSource As String
  83. Dim lngMapError As Long
  84. Dim strName As String
  85.     
  86.     'Declare error source
  87.     strErrorSource = "SharedMemMaps32.CMemoryMapFile.MapMemory"
  88.     'Ensure the Err object is clean when we start
  89.     Err.Clear
  90.     
  91.     strName = strShareName & ".map"
  92.         
  93.     'lngMaxLength is entered as "KB" so calculate correct memory size in multiples of 1024
  94.     lngMaxLength = lngMaxLength * BYTES_KB
  95.  
  96.     'Attempt to create the mapped memory File
  97.     m_hwndMemoryMap = CreateFileMapping(HFILE_NONE, _
  98.                                                                   SEC_ATTRIBUTES, _
  99.                                                                   PAGE_READWRITE, _
  100.                                                                   MAP_SIZE_HIGH, _
  101.                                                                   lngMaxLength, _
  102.                                                                   strName$)
  103.  
  104.     If m_hwndMemoryMap = 0 Then
  105.         'Raise an error
  106.        Err.Raise vbObjectError + sharedmemerr_CreateFile, strErrorSource, LoadResString(sharedmemerr_CreateFile)
  107.     End If
  108.     
  109.     'Cache the last error info
  110.     lngMapError = Err.LastDllError
  111.  
  112.     m_pMemoryMap = MapViewOfFile(m_hwndMemoryMap, _
  113.                                                        FILE_MAP_WRITE, _
  114.                                                        OFFSET_HIGH, _
  115.                                                        OFFSET_LOW, _
  116.                                                        ACCESS_ALL)
  117.     
  118.     If m_pMemoryMap = 0 Then
  119.          'Raise an error
  120.        Err.Raise vbObjectError + sharedmemerr_CreateMap, strErrorSource, LoadResString(sharedmemerr_CreateMap)
  121.     End If
  122.     
  123.     'Initialize the memory with no data
  124.     If Not lngMapError = ERROR_ALREADY_EXISTS Then
  125.         CopyMemory ByVal m_pMemoryMap, NO_DATA, BYTES_LONG
  126.     End If
  127.  
  128.     'Try to create the mutex.
  129.     m_hwndMutex = CreateMutex(SEC_ATTRIBUTES, _
  130.                                                 OWN_MUTEX_NO, _
  131.                                                 strShareName & ".mtx")
  132.  
  133.     ' Ife            TyS   EADWRITE,e & ".mtx")
  134.  
  135. All_Se******:emorreName & ".mtx")
  136.  
  137.     ' Ife            TyS   EAD     teMa.d m_pMemntm