I am trying to include an external FORTRAN-based (compiled with the Intel Fortran compiler) DLL in MATLAB. As it is external, I cannot make any adjustments to the runtime library of the DLL. Currently, I wrote an accompanying header file in C++ to be able to call the DLL. Using loadlibrary
the library is loaded into MATLAB (no errors - one warning), however, when using calllib
MATLAB crashes and does not provide an error.
I think one of the following might be the reason for this, but as I am an inexperienced in using DLL's (especially coding in C++) I did not yet find the error myself.
- There is also a .lib file that I got from the supplier, but I did not incorporate this yet in the MATLAB file or C++ header file.
- The
FILEA
andFILEB
variables are paths to two text files that are input to the DLL, I think I might not have correctly incorporated these in C++. - In the
mHeader
file (MATLAB header file)stdcall
is only mentioned in the commented section and not in the coding section.
The code for the header file in C++ and my MATLAB script is shown below:
#ifndef _MYMODEL
#define _MYMODEL
#ifdef __cplusplus
extern "C" {
#endif // _cplusplus
// Functions and data types defined
void __stdcall MYFUN(char FILEA[], char FILEB[], int *IDTask, int
*nErrorCode, int *ErrorCode, double *Props, double *Out1, double *Out2,
double *Out3, double *Out4, double *Out5);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !_MYMODEL
MATLAB (r2018b):
%% Input to model
FILEA = 'PATH\FILEA.txt';
FILEB = 'PATH\FILEB.txt';
IDTask = 1; %Multiple tasks possible in the .dll
%% Determine pointers
lpFILEA = libpointer('cstring', FILEA);
lpFILEB = libpointer('cstring', FILEB);
lpIDTask = libpointer('int32Ptr', IDTask);
lpnErrorCode = libpointer('int32Ptr');
lpErrorCode = libpointer('int32Ptr');
lpProps = libpointer('doublePtr');
lpOut1 = libpointer('doublePtr');
lpOut2 = libpointer('doublePtr');
lpOut3 = libpointer('doublePtr');
lpOut4 = libpointer('doublePtr');
lpOut5 = libpointer('doublePtr');
%% LoadLibrary
[notfound, warnings] = loadlibrary('MYMODEL.dll','MYMODEL.h' ,'mfilename', 'mHeader');
%% Call .dll
[~,~, ~, nErrorOut, ErrorCodeOut, PropsOut, Out1_, ~, ~, Out4_, Out5_] ...
= calllib('MYMODEL', 'MYFUN', lpFILEA, ...
lpFILEB, lpIDTask, lpnErrorCode, lpErrorCode, lpProps, lpOut1, ...
lpOut2, lpOut3, lpOut4, lpOut5);
Thanks in advance for your help!