How do I wrap MATLAB Compiler R2014a created C DLLs to create another DLL?

6 views (last 30 days)
I am trying to use a MATLAB Compiler R2014a created a C shared DLL file in my application. However, my application loads DLLs as a plug-in and cannot be instructed to call the required MCR initialization steps and data conversion steps required.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Sep 2019
Edited: MathWorks Support Team on 18 Sep 2019
Note that the following information is valid for MATLAB release R2014a and newer. For previous releases see:
http://www.mathworks.com/matlabcentral/answers/94715-how-do-i-wrap-matlab-compiler-4-8-r2008a-created-c-dlls-to-create-another-dll
MATLAB Compiler R2014a generated DLLs do not have the ability to enable functions calls without first initializing the MCR and the library. Addtionally, they deal primarily with mxArray data types.
To work around this, the MATLAB DLL can be wrapped by an intermediate level of code which does the following:
1. Initialize the MCR and load the MATLAB DLL (the first time it is called).
2. Convert data formats of the driver into MATLAB data (mxArray).
3. Call the MATLAB functions from the MATLAB DLL.
4. Convert MATLAB data back into data type formats that the driver can understand.
The attached file shows an example using C code.
This example was tested using MS Visual C++ 2010 Professional Edition.
Level 1 : MATLAB code
----------------------
level1.m contains the MATLAB code which implements the functionality required from the MATLAB DLL. level1.m contains a MATLAB function which takes a 2D matrix and a string. The function displays the string using a dialogue box and returns the input matrix multiplied by 2.
A DLL is compiled from this MATLAB code using the following command:
NOTE: Ensure that a level1.dll from a previous run does not exist on the path before executing the following.
mcc -v -B csharedlib:level1 level1.m
This step creates level1.dll, level1.lib and level1.h among others.
Level 2 : Wrapper code
-----------------------------
The wrapper code level2.c handles the four tasks mentioned above. When working with MSVC++, this is compiled to create level2.dll using the following command:
mbuild -v level2.c level1.lib LINKFLAGS="$LINKFLAGS /DLL /DEF:level2.def" LDEXT=".dll" CMDLINE250="mt -outputresource:$EXE';'2 -manifest $MANIFEST"
level2.def is a plain text DEF-file listing the name of the library and the functions in level2.c which need to be exported (visible) in the generated level2.dll file. Please note that level2.dll is dependent on level1.dll
If working with MinGW64, compile the DLL using:
mbuild -v -g level2.c level1.lib LDFLAGS="-shared $LDFLAGS" LDEXT=".dll"
Note that MinGW64 does not require a DEF-file.
Level 3 : Driver code
--------------------------
This code is indicative of the final driver code. No initialization steps are required and the functions can be called with C data types. This driver code, level3.c, is compiled using the following command:
mbuild -v level3.c
Note that the final execution of level3.exe requires level2.dll and level1.dll to be available in the same directory as this executable or somewhere else on the system path.
  1 Comment
Richa Gupta
Richa Gupta on 20 Jul 2016
You can use mxCreateDoubleScalar to create a scalar double mxArray. The function is unsuccessful when there is not enough free heap space to create the mxArray.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!