• Welcome to Valhalla Legends Archive.
 

Help With Porting Code From C++

Started by teK, October 02, 2004, 01:29 PM

Previous topic - Next topic

LivedKrad

Why are you using the logical "AND"? Would it not make more sense to use the appendage &?

drivehappy

@LivedKrad:
I don't believe VB uses the '&' for bitwise operations. The And in this case acts as a bitwise AND, not logical.

@dRAgoN:
I don't have VB6 installed, tried it on VB.NET and it works fine - I have no idea why it should fail.

l)ragon

im kinda sure this board was for vb 6.
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

LivedKrad

I [wasn't] saying to use & as a bitwise operator, seeing as how this isn't a bitwise operation (the string appending). All you're doing, (on the left operand of the shift operation), is appending &HF0 to the original string. In which case, you would use the appending &.

Edit in []

drivehappy

@LivedKrad:
No, the & in C++ is a bitwise AND operation - and it's &HF, not &HF0.

@dRAgoN:
Seeing as how I explicitly mentioned I did not have VB6 installed and do not see what the problem is, I suggest you stop running around in circles and tell me flat out what the issue is.

Stealth

As far as I know, & serves exclusively to concatenate strings in VB6.
- Stealth
Author of StealthBot

drivehappy

Ok, to clarify for LivedKrad:

a = (data[0] & 0xF) << 8;


Means: AND (bitwise) 0xF with data[ 0 ], store in data[ 0 ], then shift data[ 0 ] left 8 bits.
Since we're originally working in C notation there is no appending of data.

EDIT: Reformatted data[ 0 ].

LivedKrad

Once again my lack of knowledge in C++ syntax and operators fails me again. ;\

TheMinistered

#23

Public Function GamePacketSize(ByRef bytData() As Byte, ByRef lngSize As Long, ByRef lngOffset As Long) As Long
    Dim objBitwiseOperator As clsBitwiseOperator
   
    Set objBitwiseOperator = New clsBitwiseOperator
   
    If (bytData(0) < &HF0) Then
        lngSize = (bytData(0) - 1)
        lngOffset = 1
        GamePacketSize = VarPtr(bytData(1))
        Exit Function
    End If
   
    lngSize = objBitwiseOperator.ShiftLeft((bytData(0) And &HF), 8) + bytData(1) - 2
    lngOffset = 2
    GamePacketSize = VarPtr(bytData(2))
End Function



'clsBitwiseOperator

Option Explicit

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)

Private Type tPD
    hMem                As Long
    PtrToOldCode        As Long
End Type
Private ProcDetails()   As tPD

Private VTIndex         As Long
Private Code            As Byte
Private CodeSize        As Long
Private PtrToNewCode    As Long
Private PtrToMyself     As Long
Private i               As Long

Private Sub Class_Initialize()
    VTIndex = -1    'initialize index into Virtual Table
    ShiftLeft 0, 0  'this sets up m/c code and modifies the VT
    ShiftRight 0, 0  'this sets up m/c code and modifies the VT
    RotateLeft 0, 0  'this sets up m/c code and modifies the VT
    RotateRight 0, 0  'this sets up m/c code and modifies the VT
End Sub

Public Function ShiftLeft(ByVal lngValue As Long, ByVal lngShift As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3E0 8902 31C0 C21000"

End Function

Public Function ShiftRight(ByVal lngValue As Long, ByVal lngShift As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3E8 8902 31C0 C21000"

End Function

Public Function RotateLeft(ByVal lngValue As Long, ByVal lngRotate As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3C0 8902 31C0 C21000"

End Function

Public Function RotateRight(ByVal lngValue As Long, ByVal lngRotate As Long) As Long

  'this is in fact only called once during class initialize
  'subsequent calls are diverted (via the VT) to the m/c code

    DivertTo "8B442408 8B4C240C 8B542410 D3C8 8902 31C0 C21000"

End Function

Private Sub DivertTo(ByVal HexCode As String)

    VTIndex = VTIndex + 1 'inc index into VT
    ReDim Preserve ProcDetails(0 To VTIndex) 'adjust array size
   
    HexCode = Replace$(HexCode, " ", "") 'remove spaces from hex code
    CodeSize = Len(HexCode) / 2 'length of the resulting binary code (2 hex chars per byte of code)

    With ProcDetails(VTIndex)
        .hMem = GlobalAlloc(0, CodeSize) 'get memory for m/c code and save handle
        PtrToNewCode = GlobalLock(.hMem) 'get far pointer to allocated memory

        For i = 0 To CodeSize - 1
            Code = Val("&H" & Mid$(HexCode, i + i + 1, 2)) 'convert hex to binary m/c code
            RtlMoveMemory ByVal PtrToNewCode + i, Code, 1 'store it in allocated memory
        Next i

        .PtrToOldCode = VirtualTableEntry 'save old VT entry; VTIndex determines which entry
        VirtualTableEntry = PtrToNewCode 'overwrite VT entry; VTIndex determines which entry
        GlobalUnlock .hMem 'unlock memory
    End With 'PROCDETAILS(VTINDEX)

End Sub

Private Property Let VirtualTableEntry(ByVal FarPointer As Long)

    RtlMoveMemory PtrToMyself, ByVal ObjPtr(Me), 4 'get pointer to object (Me)
    RtlMoveMemory ByVal PtrToMyself + &H1C + VTIndex * 4, FarPointer, 4 'put VT entry

End Property

Private Property Get VirtualTableEntry() As Long

    RtlMoveMemory PtrToMyself, ByVal ObjPtr(Me), 4 'get pointer to object (Me)
    RtlMoveMemory VirtualTableEntry, ByVal PtrToMyself + &H1C + VTIndex * 4, 4 'get VT entry

End Property

Private Sub Class_Terminate()

    For VTIndex = VTIndex To 0 Step -1 'VTIndex still points to the last VT entry overwritten
        With ProcDetails(VTIndex)
            VirtualTableEntry = .PtrToOldCode 'restore VT entry; VTIndex determines which entry
            GlobalFree .hMem 'release memory used for m/c code
        End With 'PROCDETAILS(VTINDEX)
    Next VTIndex

End Sub