
Originally Posted by
xxdoc123
If I want to pass string data to the shared memory from the main process, how does the working process know the length of the shared string when reading data?
Now I think of this method to solve this problem...
Do you have other methods?
There is no need to apply a (non-unicode-capable) API for that.
I'd simply:
- define all Strings of a Shared-Memory-Area as fixed-length-string-members of an UDT
- then later always read (or write) the entire UDT-content from (or to) the Shared-Area via: UserDataWrite VarPtr(MyUDT), LenB(MyUDT)
- and for "InBetween" happening changes of the UDTs FixedLength-StringMembers I'd use a simple "Property-Helper" like shown below:
Code:
Option Explicit
Private Type tMyUDT
S As String * 100
End Type
Private Sub Form_Load()
Dim MyUDT As tMyUDT
Debug.Print Len(FLS(MyUDT.S)), FLS(MyUDT.S) 'print the S-member content from a fresh initialized UDT
FLS(MyUDT.S) = "123": Debug.Print Len(FLS(MyUDT.S)), FLS(MyUDT.S) 'print it after assigning "123"
FLS(MyUDT.S) = "": Debug.Print Len(FLS(MyUDT.S)), FLS(MyUDT.S) 'print it again, after assigning ""
End Sub
Private Property Let FLS(FLSmember As String, RHS As String)
If Len(FLSmember) <= Len(RHS) Then Err.Raise 7
FLSmember = RHS & vbNullChar
End Property
Private Property Get FLS(FLSmember As String) As String
Dim Pos As Long: Pos = InStr(FLSmember, vbNullChar)
If Pos > 0 Then FLS = Left$(FLSmember, Pos - 1)
End Property
Olaf