Results 1 to 8 of 8

Thread: [RESOLVED] Copymemory array

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Resolved [RESOLVED] Copymemory array

    Hello!

    Code:
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (dst As Any, ByVal iLen&)
    
      Dim X As Long, Y As Long
      ReDim Source(0 To 500, 0 To 5) As String
    
      For X = 0 To 5
        For Y = 0 To 500
          Source(Y, X) = "Item " + CStr(Y) + " in col " + CStr(X)
        Next Y
      Next X
      
      
      ReDim out(UBound(Source, 1)) As String
      
      
      CopyMemory ByVal VarPtr(out(0)), ByVal VarPtr(Source(0, 2)), 4 * (UBound(out) + 1)
      ZeroMemory ByVal VarPtr(Source(0, 2)), 4 * (UBound(out) + 1)
      Erase Source
      MsgBox "125th element: " + out(124) + vbNewLine + "126th element:" + out(125)
      
      End
    By using this code you can simply move a 2d string arrays one indexed dimension into a single dimension array.

    But if you change the variable type to Variant, everything goes to wrong. It copies the first 125 element (0-124), but after the 125th element it copies only null strings.

    Do you have any ideas how can i fix this code?

    Ok ive found it. Variant type string variants has 8 byte long pointers, but strings has 4. So i have to copy the 8 byte long pointers to get it to work.
    Last edited by Jim Davis; Oct 12th, 2007 at 10:10 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