Initialise a new VARIANT from a void* pointer + a datatype

133 Views Asked by At

I am using these two functions to read a value from a multidimensional safearray

Private Declare Sub SafeArrayGetElement Lib "OleAut32.dll" ( _
                    ByVal pSafeArray As LongPtr, _
                    ByRef rgIndices As Long, _
                    ByRef dataBuffer As Any)

Private Declare Function SafeArrayGetElemsize Lib "OleAut32.dll" (ByVal pSafeArray As LongPtr) As Long

This is an example of how I use them:

Dim arr(1 to 2, 1 to 4) As Long 'or Integer, String, Object etc. Any datatype

Dim indices() As Long
ReDim indices(1 to CountOfDimensons(arr)) 'here arr is a 2D array so there are two indices
indices(1) = 1
indices(2) = 3

Dim resultBuffer() As Byte 'somewhere to read the data from the array into, big enough to hold whatever that is
ReDim resultBuffer(1 to SafeArrayGetElemsize(ArrPtr(arr)))

SafeArrayGetElement ArrPtr(arr), indices(1), resultBuffer(1) 'equivalent to resultBuffer = arr(1,3)

Now I have a resultBuffer with some data, and I can use VarType(arr) xor vbArray to get the vartype of the data. It could be Long, Integer, Variant etc...

How can I use those two pieces of information to create a variant? My idea was to have a big select case statement on the varType, declare variables of each type. Then use memcopy to hydrate those variables. Then assign their values to a new variant. However this is very over complicated. There must be an easier way to initialise a variant from some data.

Basically cast the void* to a long* or variant* etc. and then copy that into a Variant?

0

There are 0 best solutions below