CopyMemory of a Variant holding an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TIves
    New Member
    • May 2012
    • 2

    #1

    CopyMemory of a Variant holding an Array

    Within a class module I open an ADODB recordset of a single field and then move it to a Variant with rst.GetRows

    Code:
    Public CloseArray As Variant
    Public Sub OpenRecordset()
    rst.CursorLocation = adUseClient
        rst.Open "SELECT Field5 FROM " & MarketTableName & " WHERE Field1 BETWEEN " & stringStartDate & " AND " & stringEndDate & " ORDER BY Field1", dbConn, adOpenDynamic, adLockOptimistic, adCmdText
        rst.MoveFirst
        CloseArray = rst.GetRows
    rst.Close
    End Sub
    In another function I need to get an average of a chunk of this array. I can not use looping because I can have 100 instances of the recordset each with 10,000 records. I can successfully use CopyMemory on standard arrays, but I can not get it to work on a Variant passed from a class, even when I try to get the handle of the safe-array buried within the Variant.

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
    Private Declare Function SafeArrayAccessData Lib "Oleaut32" (ByVal psa As Long, pvData As Long) As Long
    Private Declare Function SafeArrayUnaccessData Lib "Oleaut32" (ByVal psa As Long) As Long
    
    Public Function testX()
    
        Dim myArray As Variant
        Dim arrayTwo() As Double
        Dim Bkmark As Variant
        
        Dim SafeArrayPointer As Long
        Dim HResult As Long
        Dim Pointer As Long
        
        myArray = System.Market.CloseArray
    
    CopyMemory SafeArrayPointer, ByVal VarPtr(myArray) + 8, LenB(myArray(0, 0))
                    HResult = SafeArrayAccessData(SafeArrayPointer, Pointer)
                    If HRresult = 0 Then
                        CopyMemory arrayTwo(0), ByVal Pointer, LenB(myArray(0, 0))
                        HRresult = SafeArrayUnaccessData(Pointer)
                    End If
    
    End Function
    If I can just get the data into ArrayTwo I can then grab the chunk I want with a second CopyMemory and then get an average of the array.

    When I run this, I do not get any errors, but ArrayTwo is filled with incorrect data. And, I can not use a simple CopyMemory because MyArray is not a "true" array.

    Any thoughts would be greatly appreciated!

    Thanks so much,

    Trip

    p.s. this article provided some good insight, but I'm still stuck...
    http://forums.devx.com/archive/index.php/t-37031.html
Working...