Converting MATLAB Class to DLL for Use in Visual Studio 2019
Show older comments
I have completed defining subclasses and parent classes in MATLAB. The member variables of the subclass are complex structures with nested structures. The values of these member variables are obtained by calling external functions or other member functions within the class. I encapsulated the subclass into an entry function to convert it into a DLL. The input parameters of the function are passed to the constructor of the subclass, resulting in an instantiated object of the subclass. By referencing different member variables and their fields within the instantiated object, I can obtain the required values.
Now I need to use the Library Compiler to generate a DLL function for the encapsulated entry function so that I can continue development in Visual Studio 2019. Should I generate a "C shared library" or a "C++ shared library"? What type should I use to handle the return values of the DLL functions, mxArray or mwArray? How can I further process these types of return values in Visual Studio 2019 C/C++ to obtain the fields and values within the structures?
Answers (1)
Aditya Saikumar
on 22 Aug 2024
Hi Alexander,
Whether to generate C shared library or C++ shared library depends on whether the application being built in Visual Studio is in C or C++. Please take a look at the following MATALAB documentation link which provides links to examples using “Library Compiler”.
The following MATLAB documentation links provide examples of building both C shared libraries and C++ shared libraries.
- C shared library: Create a C Shared Library with MATLAB Code - MATLAB & Simulink (mathworks.com)
- C++ shared library: Deploy to C++ Applications Using mwArray API (C++03) - MATLAB & Simulink (mathworks.com)
Please take a look at the following MATLAB Answers link which provides guidance on how to integrate C/C++ shared libraries into Visual Studio.
The functions exported in a C shared library require the usage of ”mxArray” API and the ones exported in a C++ shared library usually require ”mwArray” API. Please take a look at the following MATLAB documentation links for “mxArray” and “mwArray” APIs which contain a list of functions available in the APIs along with examples of how to use them.
- “mxArray” API: https://www.mathworks.com/help/releases/R2023b/matlab/cc-mx-matrix-library.html
- “mwArray” API: https://www.mathworks.com/help/releases/R2023b/compiler_sdk/cxx/mwarray.html
Here is an example of how to access a structure returned by a MATLAB function using “mxArray” API in C.
MATLAB function:
function outputStruct = getData()
arguments (Output)
outputStruct (1,1) struct
end
outputStruct = struct();
% fields
outputStruct = struct();
outputStruct.a = 1;
sArray = struct();
sArray.index = 1;
sArray.value = 1.1;
sArray(2).index = 2;
sArray(2).value = 2.2;
sArray(3).index = 3;
sArray(3).value = 3.3;
outputStruct.structArray = sArray;
end
“Library compiler“ can be used to generate a library “getData” for the above function. Please find the attached C file (main.c in main.zip) which shows how to this library.
Following will be the output after building the project and running it:

I hope this helps!
Thank you,
Aditya
Categories
Find more on Deploy to C++ Applications Using mwArray API (C++03) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!