MATLAB Answers

loadlibrary and calllib inside a C mex routine

10 views (last 30 days)
gujax
gujax on 18 Aug 2011
Hi, I have a vendor dll which I am trying to use with Matlab. I have a C code which calls this dll using GetProcAddress and then does lot of math with the data.
Now I know that I can use loadlibrary and calllib in Matlab to do the same. But then I would like to use the rest of the C code for fast simulation. However, I need to wrap this code in Matlab mex routine because I want to develop a Matlab GUI.
So my question is - how to write a C mex file which uses this C code? Or more specifically, should I retain the GetProcAddress in the mex code or should I replace those with calllib? Thanks rajn

  0 Comments

Sign in to comment.

Answers (2)

Kaustubha Govind
Kaustubha Govind on 18 Aug 2011
I think it should be possible to continue using GetProcAddress in the MEX-function. However, remember that GetProcAddress belongs to the Windows-API, so you may have to jump through some hoops to get all the right compiler/linker options to work with the MEX command. See Custom Building MEX-Files for help.

  2 Comments

gujax
gujax on 18 Aug 2011
Thank you. I think you are correct but I still cannot figure out how. Is there another better way by using calllib instead? i.e., have Matlab call functions from .dll using calllib and in the next step call a C mex file to do the math. So for example, calllib calls a USB .dll function which starts USB data transfers. And then subsequently the C Mex file takes over to perform calculations. Finally, calllib via matlab calls the .dll to stop the USB?
Kaustubha Govind
Kaustubha Govind on 19 Aug 2011
Yes, what you described is certainly possible. But I wouldn't necessary say that it is better than performing everything in C (like the code you already have). There is some overhead involved in marshaling data from the USB DLL to MATLAB, and then from MATLAB to the C-MEX file. However, if your math operations are complex enough to outweigh the data-marshaling overhead, this alternative approach may have comparable performance.

Sign in to comment.


Philip Borghesani
Philip Borghesani on 19 Aug 2011
One possible transitional approach is to turn your c code into a dll/mex file that can be used with both loadlibrary and called directly as a mex file. See the example extern/examples/shrlib/yprime.c. It should be possible to use the USB dll from c and MATLAB with calllib at the same time allowing a gradual port of the code to MATLAB.
This approach will work will if your c code is well structured and you are proficient at c (or c++) coding.
You may find that some some of your c interfacing to the usb library can not be done as well using calllib because it is not as flexible as a c program. Working c interface code is often best left alone.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by