user3571412
user3571412

Reputation: 215

Write/Read strings to byte

Well, this is my code:

Public Sub WriteString(ByRef nString As String) 
Dim sBytes() As Byte
Dim sLength As Long

sLength = Len(nString)
sBytes = StrConv(nString, vbFromUnicode)

WriteLong sLength

If sLength <= 0 Then Exit Sub

If WriteHead + sLength - 1 > BufferSize Then Allocate sLength

CopyMemory buffer(WriteHead), sBytes(0), sLength
WriteHead = WriteHead + sLength
End Sub

Public Function ReadString(Optional MoveReadHead As Boolean = True) As String 
Dim sLength As Long
Dim sBytes() As Byte

sLength = ReadLong(False)
If sLength <= 0 Then
    If MoveReadHead Then ReadHead = ReadHead + 4
    Exit Function
End If

ReDim sBytes(sLength - 1)

CopyMemory sBytes(0), buffer(ReadHead + 4), sLength

ReadString = StrConv(sBytes, vbUnicode)
If MoveReadHead Then ReadHead = ReadHead + sLength + 4

Exit Function
End Function

The problem is, when I write a string for example "ééé" and try to read, my application crash. How I can solve this? The problem occurs in "CopyMemory".

Upvotes: 0

Views: 68

Answers (0)

Related Questions