This is the buffer I am currently using, hope it helps:Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private databuff As String
Private datalen As Integer
Private epos As Integer
Private isOutbuff As Boolean
Public Event buffError(Number As Integer, Description As String, Routine As String)
Private Sub Class_Initialize()
Call Me.Clear
End Sub
Public Sub SetOutBuff()
isOutbuff = True
End Sub
Public Sub Clear()
databuff = ""
datalen = 0
epos = 1
End Sub
Public Sub LoadBuffer(ByVal s_Data As String)
On Error GoTo Handler
Call Me.Clear
databuff = s_Data
datalen = Len(s_Data)
On Error GoTo 0
Exit Sub
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "LoadBuffer()")
End If
End Sub
Public Sub Setepos(place As Integer)
If place > 0 And place < Len(databuff) Then epos = place
End Sub
Public Function Getepos() As Integer
Getepos = epos
End Function
Public Sub SendPacket(socket As CSocket, PID As Byte)
socket.SendData Chr(&HCF) & Chr(PID)
socket.SendData MakeDBYTE(datalen + 4)
socket.SendData databuff
Call Clear
End Sub
Public Sub SendPacket2(socket As CSocket, PID As Byte, cbuff As clsBuffer)
socket.SendData Chr(&HCF) & Chr(PID)
socket.SendData MakeDBYTE(Len(cbuff.getBuffer()) + 4)
socket.SendData cbuff.getBuffer()
Call Clear
End Sub
Public Sub InsertSTRING(ByVal s_Data As String)
On Error GoTo Handler
datalen = datalen + Len(s_Data) + 1
databuff = Left(databuff, datalen) & s_Data & Chr(0)
On Error GoTo 0
Exit Sub
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "InsertSTRING()")
End If
End Sub
Public Sub InsertBYTE(ByVal b_Data As Byte)
On Error GoTo Handler
databuff = databuff & Chr(b_Data)
datalen = datalen + 1
On Error GoTo 0
Exit Sub
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "InsertBYTE()")
End If
End Sub
Public Sub InsertDBYTE(ByVal db_Data As Integer)
On Error GoTo Handler
databuff = databuff & MakeDBYTE(db_Data)
datalen = datalen + 2
On Error GoTo 0
Exit Sub
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "InsertDBYTE()")
End If
End Sub
Public Sub InsertWORD(ByVal w_Data As Long)
On Error GoTo Handler
databuff = databuff & MakeWORD(w_Data)
datalen = datalen + 4
On Error GoTo 0
Exit Sub
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "InsertWORD()")
End If
End Sub
Public Function getBuffer() As String
On Error GoTo Handler
If datalen = 0 Then Exit Function
getBuffer = Left(databuff, datalen)
On Error GoTo 0
Exit Function
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "getBuffer()")
End If
End Function
Public Function ExtractSTRING() As String
Dim ret As String
ret = Mid(databuff, epos, InStr(epos, databuff, Chr(&H0)))
ret = Left(ret, InStr(1, ret, Chr(&H0)))
epos = epos + Len(ret)
ExtractSTRING = ret
End Function
Public Function ExtractBYTE() As String
Dim ret As String
ret = Mid(databuff, epos, 1)
epos = epos + 1
ExtractBYTE = ret
End Function
Public Function ExtractDBYTE() As String
Dim ret As String
ret = Mid(databuff, epos, 2)
epos = epos + 2
ExtractDBYTE = ret
End Function
Public Function ExtractWORD() As String
Dim ret As String
ret = Mid(databuff, epos, 4)
epos = epos + 4
ExtractWORD = ret
End Function
Public Function ExtractDATA(LENGTH As Long) As String
Dim ret As String
ret = Mid(databuff, epos, LENGTH)
epos = epos + LENGTH
ExtractDATA = ret
End Function
Private Function MakeWORD(Value As Long) As String
On Error GoTo Handler
Dim Result As String * 4
CopyMemory ByVal Result, Value, 4
MakeWORD = Result
On Error GoTo 0
Exit Function
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "MakeWORD()")
End If
End Function
Private Function MakeDBYTE(Value As Integer) As String
On Error GoTo Handler
Dim Result As String * 2
CopyMemory ByVal Result, Value, 2
MakeDBYTE = Result
On Error GoTo 0
Exit Function
Handler:
If isOutbuff Then
RaiseEvent buffError(Err.Number, Err.Description, "MakeDBYTE()")
End If
End Function
Public Function GetByteAsString(var As String) As String
Dim k As Long
Dim ret As String
k = Asc(Left(var, 1))
If k < 0 Or k > 255 Then
GetByteAsString = "[error]"
Exit Function
End If
ret = Hex(k)
ret = IIf(Len(ret) = 1, "0", "") & ret
GetByteAsString = "0x" & ret
End Function