Raylene
Raylene

Reputation: 13

how to use CopyMemory to copy 2D array?

I'm not familiar with CopyMemory but would like to figure out how to calculate the length to copy a whole array. Your helps is very grateful.

assuming there is a 2D array which value type includes string ,integer, single etc. The code as below:

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

Sub Test4()
    Dim arr(1 To 3, 1 To 2) 
    arr(1, 1) = "abc"
    arr(2, 1) = "befgh"
    arr(3, 1) = 1
    arr(1, 2) = 468888
    arr(2, 2) = 999.8
    arr(3, 2) = "ijklmnopq"
    
    Dim brr(1 To 3, 1 To 2)
    CopyMemory VarPtr(brr(1, 1)), VarPtr(arr(1, 1)), 16 * Ubound(arr) * Ubound(arr,2)
End Sub

The length doesn't not cover the range of arr, and just copy some value but not all . I wonder how to calculate the length to copy all value .

Upvotes: 0

Views: 470

Answers (0)

Related Questions