Visual Basic SafeArrayGetDim

Posted on 2005-04-06
Last Modified: 2013-11-25
What value does this API return. i.e. if I want to make sure that an arrray is dimensioned can I write code like

if safearraygetdim(myarray)>0 then
   its okay to go on
end if
Question by:colindow
    LVL 8

    Assisted Solution

    An undimensioned array in VB is a null pointer so that function will cause an exception.  Try this:-

    Public Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ptr() As Any) As Long

    This returns the pointer value of the pointer held by an array variable hence:-

    IF VarPtrArray(MyArray) > 0 Then
      'Not OK
    End If

    One major draw back is that is doesn't work with an array of strings.  For that you will need a type library definition.

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Or something like...

    Option Explicit

    Private someArray() As String

    Private Sub Command1_Click()
        Dim ub As Long
        ub = safeArrayGetDim(someArray)
        If ub <> -1 Then
            MsgBox "ub = " & ub, vbInformation, "Dimensioned"
            MsgBox "Not Dimensioned"
        End If
    End Sub

    Private Function safeArrayGetDim(ByVal arr As Variant) As Long
        On Error GoTo notDimmed
        safeArrayGetDim = UBound(arr)
        Exit Function
        safeArrayGetDim = -1
    End Function

    Author Comment

    I think I follow but to presson the specifics safearraygetdim is this in the api

    Public Declare Function SafeArrayGetDim Lib "OLEaut32.dll" (ByRef saArray() As Any) As Long

    so what is the value for this api function that tells me my array is not dimensioned ?

    Does this API call work with arraysof strings? (mine are!)
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Here is an entire discussion on the topic:

    Author Comment

    Thank you. As it it happens that was the article that started me wondering what the call safearraygetdim actually returns!
    LVL 27

    Accepted Solution

        cElements As Long  ' # of elements in the array dimension
        lLbound   As Long  ' lower bounds of the array dimension
    End Type

        cDims       As Integer          ' // Count of dimensions in this array.
        fFeatures   As Integer          ' // Flags used by the SafeArray
                                        ' // routines documented below.
        cbElements  As Long             ' // Size of an element of the array.
                                        ' // Does not include size of
                                        ' // pointed-to data.
        cLocks      As Long             ' // Number of times the array has been
                                        ' // locked without corresponding unlock.
        pvData      As Long             ' // Pointer to the data.
        ' Should be sized to cDims:
        rgsabound() As SAFEARRAYBOUND  ' // One bound for each dimension.
    End Type
    Const VT_BYREF = &H4000&

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)

    Public Function IsArrayDimmed(TheArray As Variant) As Boolean
       Dim lp As Long, VType As Integer
       If Not IsArray(TheArray) Then Exit Function
       Dim nDims As Integer
       CopyMemory ByVal VarPtr(VType), ByVal VarPtr(TheArray), 2
       CopyMemory ByVal VarPtr(lp), ByVal (VarPtr(TheArray) + 8), 4
       If lp = 0 Then Exit Function
       If (VType And VT_BYREF) <> 0 Then CopyMemory ByVal VarPtr(lp), ByVal lp, 4
       If lp = 0 Then Exit Function
       CopyMemory nDims, ByVal lp, 2
       IsArrayDimmed = CBool(nDims)
    End Function

    Private Sub Command1_Click()
      Dim abArray()
      MsgBox IsArrayDimmed(abArray)
      ReDim abArray(2, 3)
      MsgBox IsArrayDimmed(abArray)
    End Sub

    Author Comment

    I'd still be interested to know what the value that the  safearraygetdim api returns actually meany but between these answersI've got enough to make my program behave as I'd like so i guess thats an answer.
    I'll split the points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now