Converting old VB6 Proj

bjmoreton

New member
Joined
Aug 11, 2011
Messages
2
Location
Sabina, Ohio, United States
Programming Experience
3-5
Hello all, I am currently in the process of converting an VB6 project of mine which is a server app I created to handle info I send to it from a client I created as well to control certain aspects of my computer while I am away. I used WinSock and a packet buffer class I created and well it doesn't work in VB.NET so I was wondering if anyone here knew how I could re-write this class to work with VB.NET so I can continue on making my project. Thanks!

VB.NET:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal numbytes As Long)

Public strBuffer As String, strBuff As String

Public Function Clear()
    strBuffer = vbNullString
End Function

Public Function ClearData()
    strBuff = vbNullString
End Function

Public Function GetData() As String
     GetData = strBuff
End Function

Public Function InsertATString(data As String)
    strBuffer = strBuffer & data & Chr(&HA)
End Function

Public Function InsertBYTE(data As Integer)
    strBuffer = strBuffer & Chr(data)
End Function

Public Function InsertBytes(data As String)
    Dim i As Long
    Dim Enqueue As String
    
    For i = 1 To Len(data) Step 3
        DoEvents
        Enqueue = Enqueue & Chr(Val("&h0" & Mid(data, i, 2)))
    Next i
    strBuffer = strBuffer & Enqueue
End Function

Public Function InsertData(data As String)
    strBuffer = strBuffer & data
End Function

Public Function InsertDWORD(data As Long)
    strBuffer = strBuffer & MakeDWORD(data)
End Function

Public Function InsertDWORDArray(data() As Long)
    Dim i As Integer
    For i = LBound(data) To UBound(data) Step 1
        DoEvents
        strBuffer = strBuffer & MakeDWORD(data(i))
    Next i
End Function

Public Function InsertNonNTString(data As String)
    strBuffer = strBuffer & data
End Function

Public Function InsertNonNTStringArray(data() As String)
    Dim i As Integer
    For i = LBound(data) To UBound(data) Step 1
        DoEvents
        strBuffer = strBuffer & data(i)
    Next i
End Function

Public Function InsertNTString(data As String)
    strBuffer = strBuffer & data & Chr(0)
End Function

Public Function InsertWORD(data As Integer)
    strBuffer = strBuffer & MakeWORD(data)
End Function

Public Function MakeDWORD(Value As Long) As String
    Dim Result As String * 4
    CopyMemory ByVal Result, Value, 4
    MakeDWORD = Result
End Function

Function MakeWORD(Value As Integer) As String
    Dim Result As String * 2
    CopyMemory ByVal Result, Value, 2
    MakeWORD = Result
End Function

Public Function rATString() As String
    On Error Resume Next
    rATString = Left(strBuff, InStr(strBuff, Chr(&HA)) - 1)
    strBuff = Mid(strBuff, Len(rATString) + 2)
End Function

Public Function rBYTE() As Byte
On Error Resume Next
    rBYTE = Asc(Left(strBuff, 1))
    strBuff = Mid(strBuff, 2)
End Function

Public Function rDWORD() As Long
    Dim lReturn As Long, strTMP As String
    strTMP = Left(strBuff, 4)
    Call CopyMemory(lReturn, ByVal strTMP, 4)
    rDWORD = lReturn
    strBuff = Mid(strBuff, 5)
End Function

Public Function rFILETIME(Optional QWORD As Boolean = False) As String
    Dim strFT() As String, strTMP As String
    If Not QWORD Then
        strFT = Split(rNTString & Space(1), Space(1))
        If strFT(0) > 2147483647 Then strFT(0) = (strFT(0) - 4294967296#)
        If strFT(1) > 2147483647 Then strFT(1) = (strFT(1) - 4294967296#)
    Else
        ReDim strFT(0 To 1)
        strFT(1) = rDWORD
        strFT(0) = rDWORD
    End If
    rFILETIME = strFT(0) & Space(1) & strFT(1)
End Function

Public Function rNonNTString() As String
    rNonNTString = Left(strBuff, 4)
    strBuff = Mid(strBuff, 5)
End Function

Public Function rNTString() As String
    'On Error Resume Next
    rNTString = Left(strBuff, InStr(strBuff, Chr(&H0)) - 1)
    strBuff = Mid(strBuff, Len(rNTString) + 2)
End Function

Public Function rVOID(Leng As Integer) As String
    If Len(strBuff) < Leng Then Leng = Len(strBuff)
    rVOID = Left(strBuff, Leng)
    strBuff = Mid(strBuff, Leng + 1)
End Function

Public Function rWORD() As Long
    Dim lReturn As Long, strTMP As String
    strTMP = Left(strBuff, 2)
    Call CopyMemory(lReturn, ByVal strTMP, 2)
    rWORD = lReturn
    strBuff = Mid(strBuff, 3)
End Function

Public Sub SendPacket(PacketID As Byte)
    'If wskTNet.State <> sckConnected Then Exit Sub
    Debug.Print "Sending:" & GetPacketName(PacketID)
    Debug.Print Chr(&HFF) & Chr(PacketID) & MakeWORD(Len(strBuffer) + 4) & strBuffer
    wskTNet.SendData Chr(&HFF) & Chr(PacketID) & MakeWORD(Len(strBuffer) + 4) & strBuffer
    Clear
End Sub

Public Function SetData(data As String)
    strBuff = data
End Function
 
I pretty much just need to know how to get CopyMemory to work with VB.NET keeps giving an error about memory being protected? Just the few functions that require CopyMemory is what I need help fixing, I tried using Marshal.copy but not sure how exactly to use it?
 
Here is how I copy my bmp to bytes and vice versa. Not sure how It can be applied to you.
Its not my code [google] but its now in my program and works fine.

VB.NET:
' Lock the data.
    Public Sub LockBitmap()
        ' Lock bitmap data.
        Dim bounds As Rectangle = New Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height)
        m_BitmapData = m_Bitmap.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
        RowSizeBytes = m_BitmapData.Stride

' Allocate room
        Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
        ReDim ImageBytes(total_size)

        ' Copy data into array
        Marshal.Copy(m_BitmapData.Scan0, ImageBytes, 0, total_size)
    End Sub
 
Back
Top