Use Fortran DLL in Matlab which has no header .h file

24 views (last 30 days)
Hi together,
my problem is the following. I am using a program called Athena Visual Studio to program a model for a chemical kinetics problem. This program generates the equivalent fortran code and creates the .for and also a .dll file in which Athena specific fortran code is included.
Now I wish to use the DLL file in Matlab R2011b for calling the function in the DLL which calculates the outputs using the passed input arguments.
What I tried so far is to write a .h header file. But I did not managed to so. Therefore I could not use the loadlibrary() function. Actually I don't know whether the DLL is a C based DLL. I guess so because Athena Visual Studio needs Microsoft Visual Studio 2005 and Intel Visual Fortran Compiler or the g95 compiler.
I could call the dll by using a COM.Application server calling an Excel file which declares the function with the Visual Basic Declare function. So I think it is an C based DLL. Am I right?
The code in Excel Visual Basic for Applications:
Option Base 1
Declare Sub myModel Lib "F:\Modellierung\Fluidised_Bed_Model_noOSMOSE\FBM_CORE_TS53_PER_M2.DLL" _
Alias "MYMODEL" (ModelInput As Double, Control As Double, TempProfile As Double, _
ModelOutput As Double, OutputMatrix As Double, _
iDid As Long, flag As Double)
But using the Excel file is not possible for me because I need to run the Matlab code on an Linux Cluster which in fact does not support Windows COM.Applivation server.
Does anybody has an idea for me how to go on?
If I may help you with more information please feel free to ask.
Thanks a lot in advance!! Kind regards
Sinan

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 23 Nov 2011
As far as I know, you do need to provide a header to load a library in MATLAB (don't know of a way to get around that). You could probably use the "dumpbin" utility that ships with Visual Studio to find the exported function prototypes.
Try:
dumpbin /EXPORTS F:\Modellierung\Fluidised_Bed_Model_noOSMOSE\FBM_CORE_TS53_PER_M2.DLL
From a Visual C++ command window to see if it lists all the exported functions. Then create a header file with this information and try using that in MATLAB.
  1 Comment
Sinan Teske
Sinan Teske on 14 Jan 2012
Accepted this answer by mistake.
The answer you can find on the latest post.

Sign in to comment.

More Answers (5)

Philip Borghesani
Philip Borghesani on 23 Nov 2011
Best guess for your function is:
void __stdcall MYMODEL(double * ModelInput, double * Control, double * TempProfile, double * ModelOutput, double * OutputMatrix, long * IDid, double * flag);
A header file is not required in that you can manually create a MATLAB prototype file but there is information available on c header files and the MATLAB prototype file format is undocumented. If you wish to play with prototype files use the mfilename option to loadlibrary.
What problems are you having with your header file?

Sinan Teske
Sinan Teske on 23 Nov 2011
Actually my problem is that I do not know how to write a proper header file and I do not have a generated header file.
Furthermore some of these double variables are 1D or 2D arrays.
Is your suggested code the header file code? And is this
void __stdcall MYMODEL(double * ModelInput, double * Control, double * TempProfile, double * ModelOutput, double * OutputMatrix, long * IDid, double * flag);
the only think I have to write?
Thanks
  2 Comments
Philip Borghesani
Philip Borghesani on 23 Nov 2011
Yes, if there is only one function you wish to call in your dll than that is the entire header file needed.
As long as you pass the correct values in from MATLAB (read the doc, libpointer(s) may be wanted for output variables) any size and shape array can be passed as a double *.
Sinan Teske
Sinan Teske on 25 Nov 2011
Thanks I will try that and will update this thread then.

Sign in to comment.


Sinan Teske
Sinan Teske on 14 Jan 2012
Hello to all. I promised an update so I do now. I solved this problem as follows:
1. Write own header file in my case it looks like this for an DLL named FBM_CORE_TS53_PER_M2.DLL with function Alias MYMODEL:
#ifndef _FBM_CORE_TS53_PER_M2_H
#define _FBM_CORE_TS53_PER_M2_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Functions and data types defined... */
void __stdcall MYMODEL(double * ModelInput, double * ControlInput, double * TempProfile, double * OutputMatrix, double * flag);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
no matter if the Input argument is a array or not. Additionally the return variables are defined as input arguments. These are memory references passed to your dll code.
2. Use such a code in Matlab calling the function in the dll.
lpModelInput = libpointer('doublePtr',ModelInput);
lpControlInput = libpointer('doublePtr',ControlInput);
lpTempProfile = libpointer('doublePtr',TempProfile);
lpOutputMatrix = libpointer('doublePtr',OutputMatrix);
lpoui= libpointer('doublePtr',flag);
loadlibrary('FBM_CORE_TS53_PER_M2.DLL','FBM_CORE_TS53_PER_M2.h');
[~,~,~,libOutputMatrix,liboui] = calllib('FBM_CORE_TS53_PER_M2','MYMODEL',lpModelInput,lpControlInput,lpTempProfile,lpOutputMatrix,lpflag);
unloadlibrary('FBM_CORE_TS53_PER_M2');
I had to unload the library because the next call of the function crashed if I did not. I could not solve that! Most probably it has to do with some referencing of the memory.
Hope this helps you. Sinan Teske
  2 Comments
lynniz
lynniz on 3 Jul 2012
Hi Sinan, I am also trying to convert VB declarations to a c header so I can load a dll in MATLAB with the c header file. Your latest update has been really helpful to me, but my question is did you have to convert any variable declarations? How do you conver them? For example, my current declaration has the following:
Attribute VB_Name = "Module1"
Public mtDataArray() As String
Public mtArrary(10) As Double
Public showgpsdata As Boolean
Declare Sub LLA2ENA Lib...
Thank you, Lin
Sinan
Sinan on 29 May 2014
Edited: Sinan on 29 May 2014
Sorry for my late reply but your minimal example is not enough to help you. You would most probably have to check in the internet how VB based DLL files are addressed to (i.e. how a header should look like). Most probably it is different to the C (Fortran) based approach above.
I guess I got what you did try to solve. In my case I did not generate the DLL in Visual Basic. The above problem description was about how to use my already generated DLL file in Excel, for example. There I did not need a separate DLL header. However, for Matlab I did need a separate DLL header file.

Sign in to comment.


Zhong Hao
Zhong Hao on 29 May 2014
Hi Sinan, I am facing the same problem, where I don't have the header file for a fortran dll.
I have tried writing my own header file using the formats from the Matlab's examples and from your update(2012). However, I have not been successful in loading the dll.
May I know what are the necessary items and the format of a header file? This is because I can't seem to find them in the documentation or online (specifically fortran dlls)?
karthik
  2 Comments
James Tursa
James Tursa on 29 May 2014
What is the signature of the function you are trying to call?
Sinan
Sinan on 29 May 2014
Edited: Sinan on 29 May 2014
It is necessary to know the function names and their arguments as well as the argument types inside the DLL. I used a software called AthenaVisualStudio to model a chemical reactor and this software allowed me to compile a DLL file. So I did know how I named the function and which arguments it had. If you do not know it the reengineering of the problem is most probably unsolvable if there are no other methods like DLL analyser software which could help out.
You can see the format of the header file I used in my post above. But this is because I know how the DLL was compiled. For Fortran the above example should work fine if there is not other stuff applied in the DLL. Old Fortran examples can use headers based on C programming language (not C++). I am not a professional programmer and Google is my friend in this case.
I think this is still a pain in the * in Matlab, would which there would be nicer and simpler solutions to interconnect different software tools. This is a basic problem of engineers when using different software solutions. I also tried to go via Microsoft Message Queueing but this was a bit too high for my programming knowledge. I would probably have to discuss with some software engineers soon.

Sign in to comment.


Zhong Hao
Zhong Hao on 30 May 2014
@James: Thank you for your reply. The function is actually a subroutine. It has two outputs of type real. It also has twelve inputs of type real and another two inputs of type integer. Therefore I think it goes something like this.
SUBROUTINE FOO(out1 real , out2 real , in1 real , in2 real , in3 real , in4 real , in5 real , in6 real , in7 real, in8 integer , in9 real , in10 integer , in11 real , in12 real)
@Sinan: Thanks. Really appreciate your honest reply. Can you describe what the first paragraph actually do? Is it absolutely necessary? Are there other declarations that I have to do?
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
and why do you repeat it?
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!