I want to create a COM object using C# and would like to call this COM object from MATLAB.
MATLAB: How to create and access a C# COM object from MATLAB
MATLAB
Related Solutions
This explains how to generate a C shared library with MATLAB Compiler 4.0 (R14) and how to use it in general in an integrated development environment (IDE) using Microsoft Visual Studio .Net 2003 as an example.
Here are the steps to create a C shared library:
1. Select the compiler to be used by MCC:
mbuild -setup
2. Compile your MATLAB files into a DLL (on Windows):
mcc -B csharedlib:mylib <MATLAB files>
The -B csharedlib option is a bundle option that expands into
-W lib:<libname> -T link:lib
The -W lib:<libname> option tells the MATLAB Compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library.
Note the directory where the Compiler puts the shared library because you will need it later on. This step will produce mylib.dll, mylib.lib, and mylib.h. You can add the -g option to produce a DLL suitable for debugging in MSVC (or your own IDE).
3. Add mylib.lib to your MSVC (or your own IDE) project.
4. Make sure to call the initialization and termination routines from your code before calling any of the exported DLL functions.
To initialize the shared library and the MCR, you need to call:
mclInitializeApplication(NULL,0);mylibInitialize();
Afterwards, you should call the termination routine:
mclTerminateApplication();mylibTerminate();
5. You can call the functions compiled from the MATLAB code by invoking the exported DLL functions from your C code. For example:
In MATLAB:
1. Write a MATLAB function named foo and save it as foo.m with the following code:
function y = foo(x)y = x+1;
2. Select the compiler to be used by MCC:
mbuild -setup
3. Use the MATLAB Compiler to compile foo.m into C code that can be included in a C shared library:
mcc -B csharedlib:foolib foo.m
This will generate the following files:
foolib.ctf
foolib.h
foolib.dll
foolib.lib
foolib.exp
foolib_mcc_component_data.c
foolib.c
foolib.exports
In Microsoft Visual C/C++ .NET 2003:
1. Start the Microsoft Visual C/C++ .NET 2003 IDE.
2. Go to FILE and NEW. In the Project Types field, select Visual C++ Projects. In the Templates field, select Win32 Console Project. In the Name field type "test". Click OK. In the Win32 application Wizard, select Application Settings. Under Additional Options, check Empty project. Click Finish.
3. Select the Solution Explorer tab. Select Source Files, select Project menu option, select Add New Item. In the Templates tab select C++ Source file, type "foowrap.c" in the File name field and click Open.
4. Enter the following code in the foowrap.c file:
/* Include the MCR header file and the library specific header file * as generated by MATLAB Compiler */#include "mclmcr.h" #include "foolib.h"#include <stdio.h>int main(){mxArray *x_ptr;mxArray *y_ptr=NULL;double *y;double ret;/* Call the MCR and library initialization functions */if( !mclInitializeApplication(NULL,0) ) { fprintf(stderr, "Could not initialize the application.\n"); exit(1); }if (!foolibInitialize()) { fprintf(stderr,"Could not initialize the library.\n"); exit(1); }/* Create an mxArray to input into mlfFoo */x_ptr = mxCreateDoubleScalar(1);/* Call the implementation function *//* Note the second input argument should be &y_ptr instead of y_ptr*/mlfFoo(1,&y_ptr,x_ptr);/* The return value from mlfFoo is an mxArray so we must extract the data from it */y = mxGetPr(y_ptr);ret = *y;/* Print a double precision number*/printf("The output of foo is %f\n",ret);/* Call the library termination function */foolibTerminate();mxDestroyArray(x_ptr);mxDestroyArray(y_ptr);mclTerminateApplication();return 0;}
5. Select your project in the Solution Explorer. From Project menu option, select Properties. Open the Linker folder and select the General category. Add the location of foolib.lib and the following
$MATLABROOT\extern\lib\win32\microsoft\msvc71
in the Additional Library Directories field. Note $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB Command Prompt.
For example, you may have
c:\foo;$MATLABROOT\extern\lib\win32\microsoft\msvc71
in the Additional Library Directories field.
Note that for MATLAB releases starting with MATLAB 7.2 (R2006a), there are no subfolders under
$MATLABROOT\extern\lib\win32\microsoft
Therefore add this folder instead of
$MATLABROOT\extern\lib\win32\microsoft\msvc71
if you are using MATLAB 7.2 or later.
6. After specifying the library directories, select the Input category and type the following libraries in the Additional Dependencies field:
mclmcrrt.lib
foolib.lib
7. Open C/C++ folder, select General. Add to Additional Include directories field:
$MATLABROOT\extern\include
Also add to the Additional Include directories field the directory in which foolib.h is located.
8. Under C/C++ folder, select Code Generation. In the Runtime Library, select Multi-threaded DLL. Click OK.
9. Go to Build and Rebuild Solution.
10. You should now have built test.exe.
11. You also need to place foolib.dll and foolib.ctf in the same location as test.exe
This solution explains how to generate a C shared library with the MATLAB Compiler 4.4 (R2006a) and call it from a Microsoft Visual Studio 2005 project.
First, the following steps summarize the process of creating a C shared library and calling it from a C program:
1. In MATLAB, select a compiler with the MBUILD command.
mbuild -setup
2. Compile your MATLAB files into a C shared library with the MCC command.
mcc -B csharedlib:mylib <MATLAB files>
The -B csharedlib option is a bundle option that expands into -W lib:<libname> -T link:lib. The -W lib:<libname> option tells the MATLAB Compiler to generate a function wrapper for a shared library and call it libname. The -T link:lib option specifies the target output as a shared library.
3. Add mylib.lib and the appropriate include directories to your MSVC project. Copy the MATLAB generated DLL files to your project directory.
Note: For versions of MATLAB prior to MATLAB 7.6 (R2008a), the CTF file generated as a part of the compilation process must also be added to the project directory. For MATLAB 7.6 (R2008a) and later releases, if you are using the -C option to disable automatic embedding of the CTF archive, you must include the CTF archive in your project directory.
4. Call the MCR (MATLAB Compiler Runtime) and library initialization and termination routines from your project code.
To initialize the shared library and the MCR, call:
mclInitializeApplication(NULL,0);mylibInitialize();
After all the calls to your shared library functions, call:
mclTerminateApplication();mylibTerminate();
5. Build and run your project in MSVC.
The following detailed steps create an example C shared library that is called from a Microsoft Visual Studio 2005 project:
1. Save the following MATLAB code to a file called "foo.m":
function y = foo(x)y = x*10;
2. Execute the MBUILD command at the MATLAB prompt:
mbuild -setup
The version number of the Microsoft Visual Studio 2005 C/C++ Compiler is 8.0. Select this compiler.
3. Generate a C shared library from foo.m with the MCC command:
mcc -B csharedlib:foolib foo.m
This will generate the following files:
foolib.ctf foolib.h foolib.dll foolib.lib foolib.exp foolib_mcc_component_data.c foolib.c foolib.exports
Note your current directory where these files are placed, as you will need it later on. You can specify the directory where the files will be placed by using the -d option with MCC. For more information about available options to the MCC command, enter the following command at the MATLAB prompt:
web([docroot,'/toolbox/compiler/f0-985134.html'])
4. In Microsoft Visual Studio 2005, go to File->New->Project. In the Project Types field, select Visual C++ Projects. In the Templates field, select Win32 Console Project. In the Name field type test. Click OK. In the Win32 application Wizard, select Application Settings. Under Additional Options, select Empty project. Click Finish.
5. Click on the Solution Explorer tab. Click Source Files, and from the Project menu, select Add New Item. In the Categories pane select C++ Source file, type "foowrap.c" in the File name field and click Add.
6. Enter the following code in the foowrap.c file:
#include "stdio.h"#include "foolib.h" /* MATLAB-generated header file */int main(){mxArray *x_ptr;mxArray *y_ptr=NULL;double *y;double ret;/* Call the MCR and library initialization functions */if( !mclInitializeApplication(NULL,0) ) { fprintf(stderr, "Could not initialize the application.\n"); exit(1); }if (!foolibInitialize()) { fprintf(stderr,"Could not initialize the library.\n"); exit(1); }/* Create an mxArray to input into mlfFoo */x_ptr = mxCreateDoubleScalar(1);/* Call the implementation function *//* Note the second input argument should be &y_ptr instead of y_ptr. */mlfFoo(1,&y_ptr,x_ptr);/* The return value from mlfFoo is an mxArray. Use mxGetpr to get a pointer to data it contains. */y = mxGetPr(y_ptr);ret = *y;/* Display the output of the call to mlfFoo. */printf("The output of foo is %f\n",ret);/* Call the library termination function */foolibTerminate();mxDestroyArray(x_ptr);mxDestroyArray(y_ptr);mclTerminateApplication();return 0;}
7. Select your project in the Solution Explorer. From Project menu, select Properties. In the Configuration Properties folder, open the Linker folder and select the General category. In the Additional Library Directories field, add the location of foolib.lib, and the following directory:
$MATLAB\extern\lib\win32\microsoft
Note that $MATLAB should be replaced with the MATLAB installation directory, as returned by typing
matlabroot
at the MATLAB command prompt.
8. Also under the Linker catagory, select the Input category. Enter the following libraries in the Additional Dependencies field:
mclmcrrt.lib foolib.lib
9. Now select the C/C++ category, and select General. To the Additional Include Directories field, add the location of foolib.lib, and the following directory:
$MATLAB\extern\include
replacing $MATLAB with the MATLAB installation directory as above.
10. Also under the C/C++ category, select Code Generation. In the Runtime Library, select Multi-threaded DLL. Click OK to close the Properties window.
11. Go to Build->Build Solution.
12. You should now have built test.exe.
13. Place foolib.dll and foolib.ctf in the same location as test.exe.
Related Question
- How to use a C++ shared library that was generated with MATLAB Compiler 4.0 (R14) with MSVC 6.0
- How to create a C shared library with MATLAB Compiler 4.15 (R2011a) that can be used in Microsoft Visual Studio 2010 Express Edition
- How to create a C – shared library with MATLAB Compiler 3.0 which can be used in other projects
Best Answer