Pass ByRef Array / safearray to COM server
显示 更早的评论
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 already tried setting feature('COM_PassSafeArrayByRef', 1), like explained in https://de.mathworks.com/matlabcentral/answers/94888-how-can-i-pass-arguments-by-reference-to-an-activex-server-from-matlab-7-0-r14?s_tid=sug_su, and also feature('COM_SafeArraySingleDim', 1) from https://de.mathworks.com/matlabcentral/answers/92424-how-can-i-pass-arguments-to-an-activex-server-from-matlab-7-0-r14-as-one-dimensional-arrays but it makes no difference.
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
回答(1 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!