MATLAB Answers

Can I call an external shared library (DLL) from a MATLAB MEX-file?

65 views (last 30 days)
I would like to call an external shared library (DLL) from a MATLAB MEX-file.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 11 Apr 2019
Edited: MathWorks Support Team on 11 Apr 2019
To link an external DLL to a MEX-file, list the DLL's .lib file on the command line.
Here is an example:
mex filename.c libname.lib
Note that when calling the MEX-file from MATLAB, the DLL should be in the same directory as the MEX-file.

  5 Comments

Show 2 older comments
Andrew Cunningham
Andrew Cunningham on 17 May 2019
I am having some problems with this in Matlab 2018a/Windows 10. I have a MEX file that depends on a DLL, say, CCC.DLL. I placed this CCC.DLL into the same directory as the MEX DLL. I was having some issues with the MEX DLL not loading, and I used the Visual C++ debugger to see what DLLs were being loaded.
To my suprise I found that when the MEX DLL was loaded, a CCC.DLL that happened to be in the system PATH was being loaded before the CCC.DLL that was in the same directory as the MEX file. This seems contrary to the normal rules of LoadLibrary but things are not quite as simple as they use to be on Windows.
Furthermore, once I removed from the PATH the directory that contained the rogue CCC.DLL, I was able to verify that when the MEX DLL loaded, it also correctly loaded it's local CCC.DLL.

Sign in to comment.

More Answers (0)

Products


Release

No release entered yet.

Community Treasure Hunt

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

Start Hunting!