Speicher zwischen Applikationen Teilen

    • VB6

      Speicher zwischen Applikationen Teilen

      Die nachfolgende Klasse dient zum vereinfachen des
      Datenaustausches zwischen zwei selbst programmierten
      Applikationen:

      Visual Basic-Quellcode

      1. Option Explicit
      2. Private Declare Function CreateFileMapping Lib "kernel32.dll" Alias _
      3. "CreateFileMappingA" (ByVal hFile As Long, _
      4. ByVal lpFileMappigAttributes As Long, _
      5. ByVal flProtect As Long, _
      6. ByVal dwMaximumSizeHigh As Long, _
      7. ByVal dwMaximumSizeLow As Long, _
      8. ByVal lpName As String) As Long
      9. Private Declare Function MapViewOfFile Lib "kernel32.dll" (ByVal hFileMappingObject As Long, _
      10. ByVal dwDesiredAccess As Long, _
      11. ByVal dwFileOffsetHigh As Long, _
      12. ByVal dwFileOffsetLow As Long, _
      13. ByVal dwNumberOfBytesToMap As Long) As Long
      14. Private Declare Function UnmapViewOfFile Lib "kernel32.dll" (ByVal lpBaseAddress As Long) As Long
      15. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
      16. Private Declare Function CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long
      17. Private Declare Function ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByRef Destination As Any, ByVal Length As Long) As Long
      18. Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
      19. Private m_hMapObject As Long
      20. Private m_lpMemory As Long
      21. Private m_strMemName As String
      22. Private m_lngBytes As Long
      23. Function Initialize(ByVal strMemName As String, ByVal lngBytes As Long) As Boolean
      24. Uninitialize
      25. m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
      26. If m_hMapObject <> 0 Then
      27. m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, 0)
      28. m_strMemName = strMemName
      29. m_lngBytes = lngBytes
      30. ZeroMemory ByVal m_lpMemory, m_lngBytes
      31. End If
      32. End Function
      33. Function GetMemory(Optional ByVal strMemName As String, Optional ByVal lngBytes As Long) As String
      34. Dim strMemory As String
      35. If Len(strMemName) > 0 And strMemName <> m_strMemName Then
      36. m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
      37. If m_hMapObject <> 0 And Err.LastDllError = 183 Then 'Err 183 = File Already Exists
      38. m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, lngBytes)
      39. strMemory = String$(lstrlen(m_lpMemory), Chr$(0))
      40. CopyMemory ByVal strMemory, ByVal m_lpMemory, Len(strMemory)
      41. End If
      42. Else
      43. strMemory = String$(lstrlen(m_lpMemory), Chr$(0))
      44. CopyMemory ByVal strMemory, ByVal m_lpMemory, Len(strMemory)
      45. End If
      46. GetMemory = strMemory
      47. End Function
      48. Sub SetMemory(ByVal strValue As String, Optional ByVal strMemName As String, Optional ByVal lngBytes As Long)
      49. strValue = strValue & Chr$(0)
      50. If m_lngBytes > 0 Then
      51. If Len(strValue) > m_lngBytes Then Exit Sub
      52. End If
      53. If Len(strMemName) > 0 And strMemName <> m_strMemName Then
      54. m_hMapObject = CreateFileMapping(&HFFFFFFFF, 0, &H4, 0, lngBytes, strMemName)
      55. If m_hMapObject <> 0 And Err.LastDllError = 183 Then 'Err 183 = File Already Exists
      56. m_lpMemory = MapViewOfFile(m_hMapObject, &H2, 0, 0, 0)
      57. CopyMemory ByVal m_lpMemory, ByVal strValue, Len(strValue)
      58. End If
      59. Else
      60. CopyMemory ByVal m_lpMemory, ByVal strValue, Len(strValue)
      61. End If
      62. End Sub
      63. Sub Uninitialize()
      64. If m_hMapObject <> 0 Then
      65. UnmapViewOfFile m_lpMemory
      66. CloseHandle m_hMapObject
      67. m_lpMemory = 0
      68. m_hMapObject = 0
      69. End If
      70. End Sub
      71. Private Sub Class_Terminate()
      72. Uninitialize
      73. End Sub


      Hier ein Beispiel zum benutzen der Klasse:

      Visual Basic-Quellcode

      1. 'Applikation 1:
      2. gMemory.Initialize "Datenaustausch", 1024 'Größe des gemeinsamen Speichers kann natürlich variieren
      3. 'Applikation 2:
      4. gMemory.SetMemory "Neue Daten", "Datenaustausch", 1024
      5. 'Applikation 1:
      6. If gMemory.GetMemory = "Neue Daten" Then '[...]



      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“ ()