Vivek Rao
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
The information is available to other-language DLLs, however, because VB
passes a SAFEARRAY, which is an OLE data construct that contains this
information. So you can write this in C or some other language and get what
you need.
Our Stamina library, for example, has the function DxArrayDimmed, which
reports the number of dimensions for an array. It was designed to detect
arrays with no dimensions, e.g. Dim Ary() As Type, which will return 0, but
it does this by getting the current number of dimensions. Stamina is a
library of 350+ functions for VB and VBA written in 32-bit assembly
language.
--
Jim Mack
MicroDexterity, Inc
PO Box 5372
Plymouth, MI 48170-5372
Sales: 888-891-0700 sa...@mdxi.com
Support: +1 734-453-5872 sup...@mdxi.com
Fax: +1 734-453-8942
r...@susq.com wrote in message <6trcef$6qt$1...@nnrp1.dejanews.com>...
I've already posted this a few times, but I hope you'll excuse me if I
do it again...
This procedure is geared towards arrays that are storable in variants,
but it's not that common with unknown UDT-arrays, so I think this is
sufficient. Also, I haven't looked at the code since I first wrote it
so there may be things in it that could be optimized. If so, go ahead
and optimize away, and please mail me a copy.
If you want to know the details behind it, check out the SAFEARRAY,
SAFEARRAYBOUND and VARIANT structs in your local COM reference (Visual
C++ help will do).
' Need to declare this once for arrays
Private Declare Function VarPtrArray Lib "msvbvm50.dll" _
Alias "VarPtr" (Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, _
ByVal ByteLen As Long)
' ABSTRACT:
' Returns the number of dimensions of
' an array. Very useful for determining
' if the array can be used with LBound()
' and UBound().
'
' Normally, the VB function IsArray()
' returns true for any non-variant array
' even if LBound() and UBound() will fail.
' With this function, you get a 0 for the
' dimensions if the array is Erase()d.
'
' INPUT:
' Any array
'
' OUTPUT:
' The number of dimensions of the array.
' This is typically 0 for Erase()d arrays,
' or arrays that haven't been ReDimmed yet.
'
' NOTES:
' I constructed this function after getting
' tired of not being able to check if an
' array of Long() could be manipulated or not.
'
' HISTORY:
' 971119: JFK; First version
'
Public Function afnArrayDims(ByRef varArray As Variant) As Long
On Error GoTo Err_afnArrayDims
Dim pArray As Long
Dim ppArray As Long
Dim ppVarStruct As Long
Dim intVarType As Integer
Dim intDims As Integer
Dim lngMaskedType As Long
intVarType = VarType(varArray)
' Check so it's actually an array
If intVarType And vbArray Then
' Get the pointer to the variant
ppVarStruct = VarPtr(varArray)
' Get the pointer to the data *inside* the variant
' The VARTYPE is 2 bytes, and each of the three
' reserved words are also 2 bytes -> 8 bytes
CopyMemory ppArray, ByVal ppVarStruct + 8, 4
' Just a precaution. Hasn't happened yet, but...
If ppArray = 0 Then Exit Function
lngMaskedType = intVarType And Not vbArray
' Check the type of the array.
If lngMaskedType = vbVariant Then
' Evidently, this is a variant array.
' We've already dereferenced the variant and
' gotten to its data - a SafeArray.
' As the first part of a safearray is the
' cDims-part, we just need to copy the cDims
' descriptor
CopyMemory intDims, ByVal ppArray, Len(intDims)
afnArrayDims = intDims
Else
' This is a safearray of some standard datatype
' that has been wrapped in a variant by the
' casting in the function call.
' We access the SafeArray struct pointed at by
' the Variant
CopyMemory pArray, ByVal ppArray, 4
If pArray Then
' The first part of the SafeArray struct is
' the cDims descriptor. Return it.
CopyMemory intDims, ByVal pArray, Len(intDims)
afnArrayDims = intDims
End If
End If
End If
Exit_afnArrayDims:
Exit Function
Err_afnArrayDims:
Err.Raise Err.Number, CLASS_NAME & ".afnArrayDims()", Err.Description
End Function
> Vivek Rao
Regards
/Joachim
--
Ubi signo?
joa...@ecs.se