I'm transposing a 2-dimensional Variant array (subtype String) and would like to speed it up using CopyMemory to copy string pointers instead of actually making copies of all the strings. (Of course, if you have a better suggestion, I'm quite open to other ways to accomplish this task.)
So first, is this feasible or will I be causing memory problems unless I actually swap the string pointers?
And second, I haven't yet made the operation work successfully, probably because I just am not practiced enough with pointer manipulation and VB arrays.
Here's my original code. I've already examined the bounds of the input array and sized a dynamic array to the appropriate, transposed size. Note that the input array is coming straight from the GetRows method of an ADODB.Recordset.
So next step, I put in the function header for the movememory API:
And I began with:
Now I know that VB strings are actually pointers to the string location, so I tried ByVal. I tried VarPtr. I tried StrPtr. I tried "ByVal Dest As Long" and same for Source. But no go (and a couple of crashes, of course).
Could someone help set me straight, not just to get working code but give me some pointers (no pun intended
) on how to approach this sort of problem in the future?
So first, is this feasible or will I be causing memory problems unless I actually swap the string pointers?
And second, I haven't yet made the operation work successfully, probably because I just am not practiced enough with pointer manipulation and VB arrays.
Here's my original code. I've already examined the bounds of the input array and sized a dynamic array to the appropriate, transposed size. Note that the input array is coming straight from the GetRows method of an ADODB.Recordset.
Code:
For iColumn = iLBound1 To iUbound1
For iRow = iLBound2 To iUbound2
vasTransposed(iRow, iColumn) = vasValues(iColumn, iRow)
Next
Next
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Dest As Any, _
Source As Any, _
ByVal bytes As Long _
)
Code:
CopyMemory vasTransposed(iRow - HeaderRow, iColumn), vasValues(iColumn, iRow), 4
Could someone help set me straight, not just to get working code but give me some pointers (no pun intended