Is it possible to pass structures to methods of a COM class in MATLAB 8.1 (R2013a)?

2 views (last 30 days)
I have a COM DLL written in VB6 and the method I want to calls requires a structure as its input. For example:
'The structure
Type myStruct
Field1 As Integer
Field2 As String
End Type
'The method which I want to call
Public Sub myFun(s As myStruct, f1 As Integer, f2 As String)
f1 = s.Field1
f2 = s.Field2
End Sub
I tried calling this with a MATLAB structure as input:
s.Field1 = int32(42);
s.Field2 = 'Hello World'
obj = actxserver('myProject.myClass')
[~,out1,out2] = obj.myFun(s,1,'');
But this throws the following error:
ERROR: Warning: ActiveX - invalid argument type or value
Error using COM.myProject_myClass/myFun
Error: Type mismatch, argument 1
How can I call this method successfully?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Feb 2021
Edited: MathWorks Support Team on 24 Feb 2021
As is noted in the documentation, Structures are not supported by MATLAB's COM Automation Client:
To work around this limitation you can wrap the COM Class in a .NET Assembly and then use MATLAB's interface to the .NET Framework instead.
1. Wrapping the COM Object in a .NET Assembly.
Wrapping the COM Object can easily be achieved using the tlbimp tool provided by Microsoft in the Windows SDK (which may also have been installed by Visual Studio).
a. If you do not already have Visual Studio and/or the Windows SDK installed, first please install the Windows SDK (
).
b. From your Start Menu open the "Visual Studio 20XX Command Prompt" or "Windows SDK 7.1 Command Prompt" depending on whether you already had Visual Studio intalled or not.
c. In the command prompt navigate to the directory where you want the wrapped Assembly to be generated.
d. Run the following command:
tlbimp "c:\full\path\to\myProject.dll" /out:myProjectWrapped
Where you replace c:\full\path\to\myProject.dll with the full path to your COM DLL and you can replace myProjectWrapped with a name of your liking.
2. Calling the wrapped Assembly from MATLAB.
We now need to load the .NET Wrapper Assembly into MATLAB:
a = NET.addAssembly('c:\full\path\to\myProjectWrapped.dll')
(Note that you do not have to assign the output but if you do, you can use a.Classes and a.Structures to inspect the actual names of the classes and structures in the Assembly which you need in the next two steps).
Once the Assembly has been loaded we can create an instance of the structure and assign some values:
s = myProjectWrapped.myStruct;
s.Field1 = 42;
s.Field2 = 'Hello World';
Also we can create an instance of the class itself:
obj = myProjectWrapped.myClassClass;
Calling the method should now work without problems:
>> [~,out1,out2] = obj.myFun(s,1,'')
out1 =
42
out2 =
Hello World

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!