Results 1 to 16 of 16

Thread: [RESOLVED] Hacking Variant

Hybrid View

  1. #1
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Hacking Variant

    Other mystery left is discrepancy between pvData and VarPtr(vB(1)) when vB = A

  2. #2

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: [RESOLVED] Hacking Variant

    Yeah, I agree. I did some more investigating and found that VarPtr(vB(0)) has a different meaning depending on the type of array that is assigned to vB!

    If an array of Variant is assigned to vB, VarPtr(vB(0)) is the address of the 1st array element (variant data type). But, if an array of Long or Double is assigned to vB, VarPtr(vB(0)) is the address of what appears to be a variant data structure, but the value of the type field makes no sense. The address of the 1st array element is offset 8 bytes. Hmmm, just thought of something to be further investigated: what does VarPtr(vB(1)) represent?

    Another curious tid bit: if an array is assigned to a vB, vB can be indexed like an array, typename indicates that vB is an array, but VarPtrArray(vB) produces an error because vB isn't an array.

    There is another very odd thing I observed. If VarPtr(vB(0)) is assigned to a variable, say p1, and 16 bytes of memory values are extracted, the result is different if p1 is used vs VarPtr(vB(0)) even though p1 and VarPtr(vB(0)) evaluate to the same number! This has got to be the oddest thing I've ever seen!

    Sure could use some Bruce McKinney expertise here. Anybody have a connection????

    Code:
    Sub ArrayPointerTest2()
        'Using ideas from Logophobic's code
        Dim p1&, p2&, lValue1&, lValue2&
        Dim dValue1#, dValue2#
        Dim bt(15) As Byte
        Dim itype1%, itype2%
        Dim A(1) As Long                            '1D array of Long
        Dim B(1) As Variant                         '1D array of Variant
        Dim vB As Variant                           'Variant
    
        A(0) = 837&                                 '1st array value
        A(1) = 58&                                  '2nd array value
        B(0) = CDbl(2.37)                           '1st array value is Double
        B(1) = CLng(12)                             '2nd array value is Long
        
        vB = B                                      'assign array of Variant to vB
        p1 = VarPtr(vB(0))                          'address 1st array element (variant)
        CopyMemory bt(0), ByVal VarPtr(vB(0)), 16   'copy bytes from VarPtr(vB(0))
        DisplayBytes bt, "Variant data structure"   'variant data structure
        CopyMemory bt(0), ByVal p1, 16              'use p1 instead of VarPtr(vB(0))
        DisplayBytes bt, "no difference"            'no difference
        CopyMemory itype1, ByVal p1, 2              '5 = double
        CopyMemory dValue1, ByVal p1 + 8, 8         '1st array value = double
        p2 = p1 + 16                                'address of 2nd array element (variant)
        CopyMemory bt(0), ByVal p2, 16              'copy bytes
        DisplayBytes bt, "Variant data structure"   'variant data structure
        CopyMemory itype2, ByVal p2, 2              '3 = long
        CopyMemory lValue1, ByVal p2 + 8, 4         '2nd array value = long
        Debug.Print p1, p2, itype1, itype2, dValue1, lValue1
        'VarPtr(vB(0)) is the address of the 1st array element
            
        vB = A                                      'assign array of Long to vB
        p1 = VarPtr(vB(0))                          'address of what?
        CopyMemory bt(0), ByVal VarPtr(vB(0)), 16   'copy bytes from VarPtr(vB(0))
        DisplayBytes bt, "What is this?"            'looks like variant data structure
        CopyMemory itype1, ByVal VarPtr(vB(0)), 2   '16387 valid data type???
        CopyMemory bt(0), ByVal p1, 16              'use p1 instead of VarPtr(vB(0))
        DisplayBytes bt, "1st 2 bytes missing!"     'missing bytes, very strange!
        CopyMemory p2, ByVal p1 + 8, 4              'address of 1st array element
        CopyMemory lValue1, ByVal p2, 4             '1st array value
        CopyMemory lValue2, ByVal p2 + 4, 4         '2nd array value
        Debug.Print p1, p2, itype1, " ", lValue1, lValue2
        'VarPtr(vB(0))is an address that is offset 8 bytes from the address of the 1st array element
    
        '05 00 00 00 00 00 00 00 F6 28 5C 8F C2 F5 02 40 << Variant data structure
        '05 00 00 00 00 00 00 00 F6 28 5C 8F C2 F5 02 40 << no difference
        '03 00 00 00 00 00 00 00 0C 00 00 00 C2 F5 02 40 << Variant data structure
        ' 77036264      77036280      5             3             2.37          12
        '03 40 00 00 00 00 00 00 20 55 97 04 00 00 00 00 << What is this?
        '00 00 00 00 00 00 00 00 20 55 97 04 00 00 00 00 << 1st 2 bytes missing!
        ' 1308148       77026592      16387                       837           58
    End Sub
    
    Private Sub DisplayBytes(btArr() As Byte, strText As String)
      Dim i As Long
      For i = LBound(btArr) To UBound(btArr)
        If btArr(i) < 16 Then Debug.Print "0";
        Debug.Print Hex(btArr(i)); " ";
      Next i
      Debug.Print "<< "; strText
    End Sub
    Last edited by VBAhack; Jun 8th, 2008 at 01:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width