Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1356
  • Last Modified:

Visual Basic SafeArrayGetDim

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
else
  stop
end if
0
colindow
Asked:
colindow
3 Solutions
 
anthonywjones66Commented:
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
  'ok
Else
  '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.

Anthony.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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"
    Else
        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
   
notDimmed:
    safeArrayGetDim = -1
End Function
0
 
colindowAuthor Commented:
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!)
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an entire discussion on the topic:
http://www.vbadvance.com/arrays.htm
0
 
colindowAuthor Commented:
Thank you. As it it happens that was the article that started me wondering what the call safearraygetdim actually returns!
0
 
ArkCommented:
Type SAFEARRAYBOUND
    cElements As Long  ' # of elements in the array dimension
    lLbound   As Long  ' lower bounds of the array dimension
End Type

Type SAFEARRAY
    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

'=========Using=========
Private Sub Command1_Click()
  Dim abArray()
  MsgBox IsArrayDimmed(abArray)
  ReDim abArray(2, 3)
  MsgBox IsArrayDimmed(abArray)
End Sub
0
 
colindowAuthor Commented:
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.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now