5

When calling external C library (DLL) from matlab, it seems that matlab will first try to generate a thunk file named as filename_thunk_pcwinXX.dll (where XX is either 32 or 64 depend on the OS) in a temp directory.

In matlab docs, it mentions that this file is an interface between external DLL and Matlab, so it doesnt not contain any other data of the original external DLL.

The problem is, i have written a library that at runtime may call other libraries, matlab will always give me some wierd results or even crash when the library is calling certain libraries (not all, and my library and other libraries here are 64-bit DLL).

And all my library functions work flawlessly when being called from other C/C++ programs instead of matlab.

I just wonder, in order to let matlab works properly, does these other libraries being called by my library at runtime need to get some sort of thunk file as well? I just cannot believe that, but I dont know the exact method of how matlab handle external libraries either.

6
  • Q: What is your OS? Windows? 32-bit or 64-bit? Which version of Windows? Q: What are your other .dll's? 32-bit or 64 bit .dll's? Q: Where is Matlib? "C:\Program files (x86)\" (32-bit) or "c:\Program files" (64-bit, on 64-bit Windows)? See also: mathworks.com/help/matlab/ref/loadlibrary.html
    – paulsm4
    Mar 10, 2013 at 22:53
  • @paulsm, OS being Windows 7-64, and Matlab is also 64-bit version, I have played with matlab for a long time, this is the first time I get such problems. Mar 11, 2013 at 0:56
  • 1
    Do the headers for your other libraries explicitly state the calling convention? The thunk file is a compatibility thing used to translate to/from MATLAB. When you use MATLAB to compile your DLL, you might not want to make any assumptions about calling convention. Indeed, you should be very explicit about everything at all times.
    – paddy
    Mar 11, 2013 at 1:20
  • "I have played with matlab for a long time, this is the first time I get such problems". Q: So "what changed"? A new version of Matlib? The first time running on Win7/64? "Something else"?
    – paulsm4
    Mar 11, 2013 at 2:09
  • @paddy, matlab build the code, I just let matlab call through loadlibrary to load an external dll(not mex files) and it seems that they will build a interface dll for each external DLL they loaded (not mex files). Mar 11, 2013 at 2:14

1 Answer 1

0

I recently had to dig into the same question. Full technical details are at the link but the short answer is:

The thunk DLL is about adjusting calling conventions

The thunk function boolint32int32doubleThunk receives its arguments in the Matlab calling convention: all arguments are passed consecutively on the stack, untyped and aligned on sizeof(size_t) (64 bytes in x64) boundaries. It also receives a function pointer to the actual DLL export, and after copying the arguments to local typed variables – calls this function with its native calling convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.