0

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".

Alex K.
  • 171,639
  • 30
  • 264
  • 288
user3571412
  • 105
  • 1
  • 9
  • 1
    What does "my application crash" mean exactly? Your code has two calls to `CopyMenory` - which one "crashes"? What does the debugger tell you when you step through the code leading up to that call? What do the variables you pass to `CopyMemory` contain when it is called? What is `WriteHead` and `ReadHead`? Where are you getting the magic number `4` that's being added everywhere? – Ken White Oct 16 '14 at 22:00
  • And what does your `CopyMemory` declare look like? – Alex K. Oct 17 '14 at 10:28

0 Answers0