Pass ByRef Array / safearray to COM server

7 views (last 30 days)
Peter Kövesdi
Peter Kövesdi on 26 Jan 2024
Answered: Peter Kövesdi on 30 Jan 2024
I have a COM object named "objPoints3d" and want to call it from Matlab. What is the matlab equivalent to pass an array? I try this and get:
>> objPoints3d.Add(3,[1,2,3])
Error using Interface.6F7F088F_F1F5_4B76_81D2_44D9A7A2A1ED/Add
Invoke Error, Dispatch Exception: Ungültiger Zeiger
(Invoke Error, Dispatch Exception: invalid pointer)
...which is at least already different from passing a scalar instead of an array:
>> objPoint3d1 = objPoints3d.Add(3, 1)
Error using
Interface.6F7F088F_F1F5_4B76_81D2_44D9A7A2A1ED/Add
Error: Type mismatch, argument 2
So, at least somehow the COM object recognizes the array, but not really.
The method is definded like this, according to the documentation:
Public Function Add( _
ByVal NumberOfValues As Long, _
ByRef Array() As Double _
) As Point3D
invoke delivers this:
>> invoke(objPoints3d)
Item = handle Item(handle, Variant)
Add = handle Add(handle, int32, SafeArray(double))
GetPointByIndex = SafeArray(double) GetPointByIndex(handle, uint32_T)
GetPointByPosition = uint32_T GetPointByPosition(handle, double, double, double)
GetDispatchByIndex = SafeArray(double) GetDispatchByIndex(handle, uint32_T)
Style = handle Style(handle, int32)
SetStyle = void SetStyle(handle, int32, handle)
Edit = void Edit(handle, int32, int32, SafeArray(double))
So, here the method definition is documented as Add = handle Add(handle, int32, SafeArray(double)). And this delivers:
>> methods(objPoints3d,'-full')
Methods for class Interface.6F7F088F_F1F5_4B76_81D2_44D9A7A2A1ED:
handle Add(handle, int32, SafeArray(double))
Edit(handle, int32, int32, SafeArray(double))
SafeArray(double) GetDispatchByIndex(handle, uint32_T)
SafeArray(double) GetPointByIndex(handle, uint32_T)
uint32_T GetPointByPosition(handle, double, double, double)
handle Item(handle, Variant)
SetStyle(handle, int32, handle)
handle Style(handle, int32)
addproperty(handle, string)
delete(handle, MATLAB array)
deleteproperty(handle, string)
MATLAB array events(handle, MATLAB array)
MATLAB array get(handle)
MATLAB array get(handle, MATLAB array, MATLAB array)
MATLAB array get(handle vector, MATLAB array, MATLAB array)
MATLAB array invoke(handle)
MATLAB array invoke(handle, string, MATLAB array)
MATLAB array loadobj(handle)
release(handle, MATLAB array)
MATLAB array saveobj(handle)
MATLAB array set(handle)
MATLAB array set(handle, MATLAB array, MATLAB array)
MATLAB array set(handle vector, MATLAB array, MATLAB array)
In Visual Basic I can successfully call this method with this:
Dim dblArray(0 To 3) As Double
dblArray(0) = 0.123
dblArray(1) = 0.345
dblArray(2) = 0.33456
objPoint3d1 = objPoints3d.Add(3, dblArray)
Even if I try to pass back the output of above Method: SafeArray(double) GetPointByIndex(handle, uint32_T)), I get the same:
>> a=objPoints3d.GetPointByIndex(0)
a =
0.0268 0.0583 0.0240
>> objPoints3d.Add(3,a)
Error using Interface.6F7F088F_F1F5_4B76_81D2_44D9A7A2A1ED/Add
Invoke Error, Dispatch Exception: Ungültiger Zeiger
Actually, I'd suppose, a should now have the exact Type that the Add method wants: SafeArray(double).
I can successfully invoke a similar method of a different object from matlab:
objLine3d1 = objLines3d.Add(-0.1, 0.03, 0.05, 0.05, 0.06, 0.07)
But there the parameters are all scalars, defined like this:
Public Function Add( _
ByVal Startx As Double, _
ByVal Starty As Double, _
ByVal Startz As Double, _
ByVal Endx As Double, _
ByVal Endy As Double, _
ByVal Endz As Double _
) As Line3D

Answers (1)

Peter Kövesdi
Peter Kövesdi on 30 Jan 2024
Ahh, finally, I got it!
It's a combination of two things:
AND
- I have to make the given array a column vector, not a row vector, i.e. objPoints3d.Add(3,[1,2,3]') instead of objPoints3d.Add(3,[1,2,3])
It works only with these two things in combination. I tried them both before several times, but accidently obviously only seperately until now.

Tags

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!