将字符串写入/读取字节

时间:2014-10-16 21:12:23

标签: string vb6 byte copymemory

嗯,这是我的代码:

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

问题是,当我写一个字符串例如“ééé”并尝试阅读时,我的应用程序崩溃了。我怎么能解决这个问题?问题出现在“CopyMemory”中。

0 个答案:

没有答案