MATLAB Answers

Mex files call DLL generated by .m file

11 views (last 30 days)
Qun Wu
Qun Wu on 24 Aug 2017
Answered: Harsheel on 1 Sep 2017
I have a myfile6.m function, it is a generic function with 1 input and 1 output. By following command:
mcc -W cpplib:YourLib -T link:lib myfile6.m
a bunch of files are generated:
And then I try to link the .dll in my Mex file. I watched a lot of tutorial videos. One method is using LoadLibrary function, and a pointer which is to the address of the exported function in the DLL. The code looks like this:
#include <mex.h>
#include <windows.h>
typedef int(__stdcall *func_ptr)();
HINSTANCE hGetProcIDDLL = LoadLibrary(".dll file path") // double back slash
However, the example in videos are relatively simple. They have an explicit function in the source cpp file. Thus in this condition, we can directly use GetProcAddress function as following:
func_ptr funP = (fun_ptr)GetProcAddress(hGetProcIDDLL, "DLLfun");
When I open the generated source cpp file. It looks not easy since I am newie for matlab coding. I do not know how to put the second argument of the GetProcAddress function. I will upload the .cpp file and .h file. Could you please give me some suggestions? Or if the entire idea is wrong, please let me know. Thank you.


Sign in to comment.

Accepted Answer

Harsheel on 1 Sep 2017
You cannot load a MATLAB Compiler SDK generated shared library back into MATLAB. This is currently unsupported and is mentioned in the doc here.
You can, however, use the .dll from other applications. A complete example is documented here .
All the sample code from above link is available in the $matlabroot\extern\examples\compilersdk folder.


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by