Memory-mapped files provide a way to look at a file as a chunk of memory. This feature is very useful in languages that support examining memory at arbitrary addresses. You map the file and get back a pointer to the mapped memory. You can simply read or write to memory from any location in the file mapping, just as you would from an array. When you’ve processed the file and closed the file mapping.
Here is the basic steps to share data between different processes
- Create a memory mapped file using CreateFileMapping API which will return a handle to memory mapped file. - After you get valid file handle from CreateFileMapping you can call MapViewOfFile to map entire file into your process address space. If MapViewOfFile call is successful then it will return memory address of shared memory location. - You can now write or read data to the the shared memory location. - Call UnmapViewOfFile if you dont need shared memory anymore.
Note : You can use OpenFileMapping function to get handle of existing shared memory file. |
Click here to copy the following block | Option Explicit
Const sMapName = "TestSharedMap"
Const offset_intSharedData = 0 Const offset_lngSharedData = 2 Const offset_boolSharedData = 6 Const offset_bytArrSharedData = 7 Const offset_strSharedData = 13
Const PAGE_READONLY As Long = &H2 Const PAGE_READWRITE As Long = &H4 Const PAGE_WRITECOPY As Long = &H8
Const FILE_MAP_COPY As Long = 1 Const FILE_MAP_WRITE As Long = 2 Const FILE_MAP_READ As Long = 4 Const FILE_MAP_ALL_ACCESS As Long = FILE_MAP_WRITE
Const INVALID_HANDLE_VALUE As Long = -1
Private Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" ( _ ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, _ ByVal lpName As String) As Long
Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" ( _ ByVal hFileMapTable As Long, _ ByVal lpFileMappingAttributes As Long, _ ByVal flProtect As Long, _ ByVal dwMaximumSizeHigh As Long, _ ByVal dwMaximumSizeLow As Long, _ ByVal lpName As String) As Long
Private Declare Function MapViewOfFile Lib "kernel32" ( _ ByVal hFileMapTableMappingObject As Long, _ ByVal dwDesiredAccess As Long, _ ByVal dwFileOffsetHigh As Long, _ ByVal dwFileOffsetLow As Long, _ ByVal dwNumberOfBytesToMap As Long) As Long
Private Declare Function UnmapViewOfFile Lib "kernel32" ( _ lpBaseAddress As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _ ByVal hObject As Long) As Long
Private Declare Sub CopyMemoryWrite Lib "kernel32" Alias "RtlMoveMemory" ( _ ByVal Dst As Long, _ pSrc As Any, _ ByVal ByteLen As Long)
Private Declare Sub CopyMemoryRead Lib "kernel32" Alias "RtlMoveMemory" ( _ pDst As Any, _ ByVal Src As Long, _ ByVal ByteLen As Long)
Dim intSharedData As Integer Dim lngSharedData As Long Dim boolSharedData As Boolean Dim bytArrSharedData(0 To 5) As Byte Dim strSharedData As String
Dim hFileMapTable As Long, hMap As Long
Function OpenSharedMap(Mapname As String) As Boolean hFileMapTable = OpenFileMapping(FILE_MAP_ALL_ACCESS, False, Mapname) If hMap = 0 Then OpenSharedMap = False Exit Function Else hMap = MapViewOfFile(hFileMapTable, FILE_MAP_WRITE, 0, 0, 0) If hMap = 0 Then MsgBox "MapViewOfFile failed - LastError: " & Hex(Err.LastDllError) Exit Function End If OpenSharedMap = True End If End Function
Sub CreateSharedMap()
If hFileMapTable <= 0 Then hFileMapTable = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, _ 4096, sMapName) If hFileMapTable = 0 Then MsgBox "CreateFileMapping failed - LastError: " & Hex(Err.LastDllError) Exit Sub End If End If
If hMap <= 0 Then hMap = MapViewOfFile(hFileMapTable, FILE_MAP_WRITE, 0, 0, 0) If hMap = 0 Then MsgBox "MapViewOfFile failed - LastError: " & Hex(Err.LastDllError) Exit Sub End If End If End Sub
Sub DeleteSharedMap() If hMap = 0 Then Exit Sub
UnmapViewOfFile hMap CloseHandle hFileMapTable End Sub
Sub ReadFromSharedMap() If hMap = 0 Then Exit Sub
Dim a As Byte CopyMemoryRead intSharedData, hMap + offset_intSharedData, Len(intSharedData) CopyMemoryRead boolSharedData, hMap + offset_boolSharedData, Len(boolSharedData) CopyMemoryRead lngSharedData, hMap + offset_lngSharedData, Len(lngSharedData) CopyMemoryRead bytArrSharedData(0), hMap + offset_bytArrSharedData, Len(bytArrSharedData(0)) * (UBound(bytArrSharedData) + 1)
Dim sLen As Long CopyMemoryRead sLen, hMap + offset_strSharedData, 4 If sLen > 0 Then strSharedData = String$(sLen, 0) CopyMemoryRead ByVal StrPtr(strSharedData), hMap + offset_strSharedData + 4, sLen * 2 End If End Sub
Sub WriteToSharedMap() Dim i
If hMap = 0 Then Exit Sub
intSharedData = Rnd * 1000 lngSharedData = Rnd * 100000 boolSharedData = IIf((Rnd * 10) Mod 2 = 0, True, False) For i = 0 To UBound(bytArrSharedData) bytArrSharedData(i) = Rnd * 255 Next
Dim strTmp As String, sLen As Long strTmp = "Test string " & String$(10, Chr(65 + Rnd * 26)) sLen = Len(strTmp)
CopyMemoryWrite hMap + offset_intSharedData, intSharedData, Len(intSharedData) CopyMemoryWrite hMap + offset_boolSharedData, boolSharedData, Len(boolSharedData) CopyMemoryWrite hMap + offset_lngSharedData, lngSharedData, Len(lngSharedData) CopyMemoryWrite hMap + offset_bytArrSharedData, bytArrSharedData(0), Len(bytArrSharedData(0)) * (UBound(bytArrSharedData) + 1) CopyMemoryWrite hMap + offset_strSharedData, sLen, 4 CopyMemoryWrite hMap + offset_strSharedData + 4, ByVal StrPtr(strTmp), sLen * 2 End Sub
Sub RefreshData() Dim i, strArr As String If hMap = 0 Then Exit Sub Text1 = "" Text1 = Text1 & "Map Name : " & " [Address: &H" & Hex(hMap) & "] " & sMapName & vbCrLf & vbCrLf Text1 = Text1 & "Integer : " & " [Address: &H" & Hex(hMap + offset_intSharedData) & "] " & intSharedData & vbCrLf Text1 = Text1 & "Long : " & " [Address: &H" & Hex(hMap + offset_lngSharedData) & "] " & lngSharedData & vbCrLf Text1 = Text1 & "Boolean : " & " [Address: &H" & Hex(hMap + offset_boolSharedData) & "] " & CBool(boolSharedData And &HFF) & vbCrLf
For i = 0 To UBound(bytArrSharedData) strArr = strArr & bytArrSharedData(i) & " " Next Text1 = Text1 & "Byte Array : " & " [Address: &H" & Hex(hMap + offset_bytArrSharedData) & "] " & strArr & vbCrLf Text1 = Text1 & "String : " & " [Address: &H" & Hex(hMap + offset_strSharedData) & "] " & strSharedData & vbCrLf End Sub
Private Sub Command2_Click() Call WriteToSharedMap End Sub
Private Sub Timer1_Timer() Call ReadFromSharedMap Call RefreshData End Sub
Private Sub Form_Load() Me.Caption = Me.Caption & " Application Started at [" & Now & "]" If OpenSharedMap(sMapName) = False Then Call CreateSharedMap End If End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Call DeleteSharedMap End Sub |
|