home *** CD-ROM | disk | FTP | other *** search
Visual Basic class definition | 2002-03-03 | 5.3 KB | 137 lines |
- VERSION 1.0 CLASS
- BEGIN
- MultiUse = -1 'True
- Persistable = 0 'NotPersistable
- DataBindingBehavior = 0 'vbNone
- DataSourceBehavior = 0 'vbNone
- MTSTransactionMode = 0 'NotAnMTSObject
- END
- Attribute VB_Name = "CMemoryMapFile"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = False
- Attribute VB_Exposed = False
- Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
- Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
- '********************************************************************************************
- ' CMemoryMapFile Class Definition
- '
- ' This class encapsulates the entire process of creating shared memory,
- ' creating synchronization objects, and reading and writing to the memory.
- '
- ' A class instance represents a single shared memory file mapping.
- '
- ' Instancing is set to: 1 - Private Use
- '********************************************************************************************
- Option Explicit
-
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- '^ Magic numbers for file mapping API calls
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- 'Declare a constant of 0 for the security attributes
- Private Const SEC_ATTRIBUTES As Long = 0
-
- 'Declare a constant of -1 for the hFile handle because these functions
- 'will read/write blocks of memory directly, with no use of a file.
- Private Const HFILE_NONE As Long = -1
- 'Declare a constant of 0 for the memory File maximum size highword
- Private Const MAP_SIZE_HIGH As Long = 0
- 'Declare constants for memory offsets - none are used
- Private Const OFFSET_HIGH As Long = 0
- Private Const OFFSET_LOW As Long = 0
- ''Declare constants for memory access - all of it
- Private Const ACCESS_ALL As Long = 0
-
- 'Declare const for mutex initial ownership
- Private Const OWN_MUTEX_NO As Boolean = False
- 'Declare const for maximum wait time for a mutex - msecs
- Private Const WAIT_MAX_MUTEX As Long = 5000
-
- 'Declare const for semaphore access priviliges - full access
- Private Const SEM_ACCESS_FULL As Long = -1
- 'Declare const for semaphore inheritence - cannot be inherited
- Private Const CANT_INHERIT As Boolean = False
- 'Declare const for maximum wait time for a semaphore - msecs
- Private Const WAIT_MAX_SEM As Long = 1000
-
- 'Declare const for sleep time if required - msecs
- Private Const NAP_TIME As Long = 100
-
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- '^ Declares for various objects and handles
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- 'Memory File handles
- Private m_hwndMemoryMap As Long
- Private m_pMemoryMap As Long
- '
- 'Synchronization object handles
- Private m_hwndMutex As Long
- Private m_hwndSemaphore As Long
-
- 'Maximum number of semaphores to create
- 'also represents the maximum threads that can
- 'use a given shared memory File.
- Private All_Semaphores As Long
-
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- '^ Memory map file creation function
- '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
- Public Sub MapMemory(strShareName As String, lngMaxLength As Long)
- On Error GoTo CatchErr
- Dim strErrorSource As String
- Dim lngMapError As Long
- Dim strName As String
-
- 'Declare error source
- strErrorSource = "SharedMemMaps32.CMemoryMapFile.MapMemory"
- 'Ensure the Err object is clean when we start
- Err.Clear
-
- strName = strShareName & ".map"
-
- 'lngMaxLength is entered as "KB" so calculate correct memory size in multiples of 1024
- lngMaxLength = lngMaxLength * BYTES_KB
-
- 'Attempt to create the mapped memory File
- m_hwndMemoryMap = CreateFileMapping(HFILE_NONE, _
- SEC_ATTRIBUTES, _
- PAGE_READWRITE, _
- MAP_SIZE_HIGH, _
- lngMaxLength, _
- strName$)
-
- If m_hwndMemoryMap = 0 Then
- 'Raise an error
- Err.Raise vbObjectError + sharedmemerr_CreateFile, strErrorSource, LoadResString(sharedmemerr_CreateFile)
- End If
-
- 'Cache the last error info
- lngMapError = Err.LastDllError
-
- m_pMemoryMap = MapViewOfFile(m_hwndMemoryMap, _
- FILE_MAP_WRITE, _
- OFFSET_HIGH, _
- OFFSET_LOW, _
- ACCESS_ALL)
-
- If m_pMemoryMap = 0 Then
- 'Raise an error
- Err.Raise vbObjectError + sharedmemerr_CreateMap, strErrorSource, LoadResString(sharedmemerr_CreateMap)
- End If
-
- 'Initialize the memory with no data
- If Not lngMapError = ERROR_ALREADY_EXISTS Then
- CopyMemory ByVal m_pMemoryMap, NO_DATA, BYTES_LONG
- End If
-
- 'Try to create the mutex.
- m_hwndMutex = CreateMutex(SEC_ATTRIBUTES, _
- OWN_MUTEX_NO, _
- strShareName & ".mtx")
-
- ' Ife TyS EADWRITE,e & ".mtx")
-
- All_Se******:emorreName & ".mtx")
-
- ' Ife TyS EAD teMa.d m_pMemntm