Die nachfolgende Klasse dient zum vereinfachen des
Datenaustausches zwischen zwei selbst programmierten
Applikationen:
Hier ein Beispiel zum benutzen der Klasse:
Keywords: Visual Basic 6, VB 6, mehrere Programme, Speicher teilen, Variablen übergeben, Daten übergeben, Datenaustausch, Funktion, Function, SetMemory, GetMemory, API, CreateFileMapping, MapViewOfFile, CopyMemory, RTLMoveMemory
Datenaustausches zwischen zwei selbst programmierten
Applikationen:
Visual Basic-Quellcode
- Option Explicit
- Private Declare Function CreateFileMapping Lib "kernel32.dll" Alias _
- "CreateFileMappingA" (ByVal hFile As Long, _
- ByVal lpFileMappigAttributes 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.dll" (ByVal hFileMappingObject 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.dll" (ByVal lpBaseAddress As Long) As Long
- Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
- Private Declare Function CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long
- Private Declare Function ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByRef Destination As Any, ByVal Length As Long) As Long
- Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
- Private m_hMapObject As Long
- Private m_lpMemory As Long
- Private m_strMemName As String
- Private m_lngBytes As Long
- Function Initialize(ByVal strMemName As String, ByVal lngBytes As Long) As Boolean
- Uninitialize
- m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
- If m_hMapObject <> 0 Then
- m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, 0)
- m_strMemName = strMemName
- m_lngBytes = lngBytes
- ZeroMemory ByVal m_lpMemory, m_lngBytes
- End If
- End Function
- Function GetMemory(Optional ByVal strMemName As String, Optional ByVal lngBytes As Long) As String
- Dim strMemory As String
- If Len(strMemName) > 0 And strMemName <> m_strMemName Then
- m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
- If m_hMapObject <> 0 And Err.LastDllError = 183 Then 'Err 183 = File Already Exists
- m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, lngBytes)
- strMemory = String$(lstrlen(m_lpMemory), Chr$(0))
- CopyMemory ByVal strMemory, ByVal m_lpMemory, Len(strMemory)
- End If
- Else
- strMemory = String$(lstrlen(m_lpMemory), Chr$(0))
- CopyMemory ByVal strMemory, ByVal m_lpMemory, Len(strMemory)
- End If
- GetMemory = strMemory
- End Function
- Sub SetMemory(ByVal strValue As String, Optional ByVal strMemName As String, Optional ByVal lngBytes As Long)
- strValue = strValue & Chr$(0)
- If m_lngBytes > 0 Then
- If Len(strValue) > m_lngBytes Then Exit Sub
- End If
- If Len(strMemName) > 0 And strMemName <> m_strMemName Then
- m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
- If m_hMapObject <> 0 And Err.LastDllError = 183 Then 'Err 183 = File Already Exists
- m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, 0)
- CopyMemory ByVal m_lpMemory, ByVal strValue, Len(strValue)
- End If
- Else
- CopyMemory ByVal m_lpMemory, ByVal strValue, Len(strValue)
- End If
- End Sub
- Sub Uninitialize()
- If m_hMapObject <> 0 Then
- UnmapViewOfFile m_lpMemory
- CloseHandle m_hMapObject
- m_lpMemory = 0
- m_hMapObject = 0
- End If
- End Sub
- Private Sub Class_Terminate()
- Uninitialize
- End Sub
Hier ein Beispiel zum benutzen der Klasse:
Keywords: Visual Basic 6, VB 6, mehrere Programme, Speicher teilen, Variablen übergeben, Daten übergeben, Datenaustausch, Funktion, Function, SetMemory, GetMemory, API, CreateFileMapping, MapViewOfFile, CopyMemory, RTLMoveMemory
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Lupus“ ()