Fortran DLL
Fortran DLL
creating a DLL with Fortran and using the DLL in Visual Basic. I am using Visual Studio 2005.
I began by opening a Fortran Dynamic Link Library type project template in Visual Studio. I have modified
the Subroutine to produce a trivial Fuction that returns a value. The Fortran code is:
Function FortranDLL
! Expose subroutine FortranDLL to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT::FortranDLL
FortranDLL = 3.141
Return
end
I then added a standard Windows Application to the project. I added the following Visual Basic Declare
statement to declare my test DLL and call the DLL from the Form's Click Event.
When I run the VB code above I receive the following error message stating that the program is unable to
find the EntryPoint for the DLL.
Can you point me to information that will help me create a simple Function in the form of a DLL with Intel's
Fortran Compiler and call that Function from Visual Basic using Visual Studio 2005? If it is relatively easy
to do I will purchase the Compilier for use in my programs.
Thank You.
Jim Denny
here are two additional things you have to do, both of which can be addressed by adding the following line to
the Fortran code:
First, Intel Visual Fortran converts all routine names to uppercase and (on IA-32), prefixes the routine name
with an underscore. So what is created is the symbol _FORTRANDLL which does not match what Visual
Basic is looking for.
The second problem is that there are two different conventions for calling routines on 32-bit Windows,
STDCALL and C. VB uses STD CALL and Intel Visual Fortran uses C. The line I suggested above selects
STDCALL. I added REFERENCE also in case you change the code to pass arguments, as STDCALL implies
pass by value.
You can get more information in the mixed-language programming chapter of the on-disk documentation.
Thanks Steve.
That was a big help. Works like a charm. That got me pointed in the right direction. I know the Fortran
compiler will help significantly speed up intensive numerical analysis. It appears that developing DLL's with
Fortran for use in VB is fairly straight forward and might be a viable option for me. I think I can figure out
the rest from here. As they say, "I know just enough to be dangerous".
Thanks,
Thanks Steve,
I have also been trying to do about the same with Fortran dll's called from VB in Visual Studio 2005.
What would be the Fortran Code to call a Fortran dll from a Fortran console application in Visual Studio
2005.
Thanks again,
Norm
Norm, I'm unsure what you're asking. You say you're using VB and then ask about calling Fortran from
Fortran? In the latter case, nothing special is needed other than to link with the .lib created when the DLL
was built, and to ensure that the Fortran executable links to the same DLL run-time libraries as the Fortran
DLL.
--------
Steve
dear friends,
I solwe the same problem using fortran 9.1 and visual basic 2005. Your explaining is not understandable for
me. I wrote this fortran and basic code in my visual studo 2005, but a got the same error mesage
Message="Unable to find an entry point named 'FortranDLL' in DLL 'C:VB .NET ProjectsFotran DLLFortran
DLLDebugfortran dll.dll'."
Source="Fortran DLL"
hear is you repaired codes
Function FortranDLL
!DEC$ ATTRIBUTES STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
FortranDLL = 3.141
Return
end
and VB code
ou are missing the DLLEXPORT attribute in your Fortran code. Use this:
no precluding,sorry
where is the problem?
When I build your test program with the correct attributes, I get a DLL which contains a "FortranDLL"
routine. So the problem is somewhere else - perhaps your VB code is referring to a different DLL. I cannot
tell what is wrong from the information you have given us.
One thing you can do is to download the free tool Dependency Walker and use it to look at your DLL. Make
sure that it shows that FortranDLL is an exported function and that it does not show any errors.
Function FortranDLL(x,y)
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
real x
real y
FortranDLL = 3.141*x*y
Return
end
In the VB declaration of FortranDLL, change ByVal to ByRef. The Fortran default is to pass variables by
reference. If you had used STDCALL without REFERENCE, that would change the default to by value, but
adding REFERENCE changes it back.
--------
Steve
Hi Steve,
FUNCTION FORTRANCALL(R1,NUM)
NUM = R1 - 25
End FUNCTION
Module Module1
End Module
Here's the corresponding VB Sub:
Private Sub Button1_Click(ByVal sender As System.Object,...
...ByVal e As System.EventArgs) Handles Button1.Click
Dim R1 As Integer
R1 = 75
NUM = 20
TextBox2.Text = Str$(R1)
TextBox3.Text = Str$(NUM)
TextBox1.Text = Str(NUM)
TextBox4.Text = Str$(R1)
TextBox5.Text = Str$(NUM)
End Sub
by the VB program.
Thanks.
I agree with Anthony, but in this case the real problem is that you have not declared R1 as an
INTEGER in the Fortran routine. That means it is implicitly REAL giving you very odd results.
You definitely should make the Fortran code a subroutine as you're going to corrupt the floating point
stack if you don't.
..yet another reason for including IMPLICIT NONE in all one's routines etc.
There are two additional things you have to do, both of which can be addressed by adding the following line
to the Fortran code:
First, Intel Visual Fortran converts all routine names to uppercase and (on IA-32), prefixes the routine name
with an underscore. So what is created is the symbol _FORTRANDLL which does not match what Visual
Basic is looking for.
The second problem is that there are two different conventions for calling routines on 32-bit Windows,
STDCALL and C. VB uses STD CALL and Intel Visual Fortran uses C. The line I suggested above selects
STDCALL. I added REFERENCE also in case you change the code to pass arguments, as STDCALL implies
pass by value.
You can get more information in the mixed-language programming chapter of the on-disk documentation.
hi everybody:
i´ve got a questin related to this topyc. if the created fortran dll uses a numerical library (i have in the code:
use numerical_libraries), when i call the created dll from visual basic, it can´t find it in a pc where fortran is
not installed. that may be because it can´t find the numerical libraries. where and which files do i have to add
to this pc so that it can work properly.
any idea?
Thanks
As with any DLL library you link against, you need to have a copy of that DLL on the end-user system in a
place Windows knows where to find it. If you have specified that you are building against the DLL version of
IMSL, you will need the appropriate IMSL DLL (which is NOT named "numerical_libraries"). I suggest that
you use Dependency Walker to determine what the name of the DLL is, as VB won't tell you. Please note
that your application must use IMSL and not just provide IMSL functionality to some other program,
Hallo, Steve,
Function FortranDLL
! Expose subroutine FortranDLL to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
FortranDLL = 3.141
Return
end
In running, at first microsoft Visual Studio says the project is old, and new compile, then
"BadImaheFormatException" will not deal, and
"An attempt was made to load a program with an incorrect format. (Exception from HRESULT:
0x8007000B)"
regards,
have downloaded the trial version of Intel's Visual Fortran Compiler and I am investigating the possibility of
creating a DLL with Fortran and using the DLL in Visual Basic. I am using Visual Studio 2005.
I began by opening a Fortran Dynamic Link Library type project template in Visual Studio. I have modified
the Subroutine to produce a trivial Fuction that returns a value. The Fortran code is:
Function FortranDLL
Public
Class Form1
Declare Auto Function TestDLL Lib "C:VB .NET ProjectsFotran DLLFortran DLLDebugfortran dll.dll"
Alias "FortranDLL" () As Single
Test = TestDLL
End Sub
End
Class
When I run the VB code above I receive the following error message stating that the program is unable to
find the EntryPoint for the DLL.
System.EntryPointNotFoundException was unhandled
Message="Unable to find an entry point named 'FortranDLL' in DLL 'C:VB .NET ProjectsFotran
DLLFortran DLLDebugfortran dll.dll'."
Source="Fortran DLL"
Can you point me to information that will help me create a simple Function in the form of a DLL with Intel's
Fortran Compiler and call that Function from Visual Basic using Visual Studio 2005? If it is relatively easy
to do I will purchase the Compilier for use in my programs.
Thank You.
Jim Denny
Many thanks, Steve Lionel. I am a Java programmer faced with the need to call FORTRAN dlls from
VB .NET in Visual Studio 2008. I spent many hours obtaining a lot of bad advice before I found this
excellent, concise advice.
Problem
A mixed VB/IVF project in which VB passed an array to Fortran DLL worked when built in ia32
configuration but fails when built with x64 configuration.
Environment
Windows x64, Visual Studio 2005 or 2008 with VB component and IVF component installed. x64 project
configurations.
Root Cause
Many VB users have been passing the first element of the array ByRef with the follow syntax:
Module Module1
Private Declare Sub FortranDLL Lib "SampleFortranDLL.dll" Alias "FORTRANDLL" (ByRef Array1 As
Int32, ByRef Int32 As Int32)
Sub Main()
Try
Dim Test As Int32() = {1, 2, 3, 4, 5, 6}
FortranDLL(Test(0), Test.Length)
This works on ia32 but apparently just by luck. This syntax is incorrect for x64.
Resolution
It seems VB handles array passing differently in x64 than Ia32. I would refer you to the following articles:
http://blogs.msdn.com/robgruen/archive/2004/04/30/124076.aspx
http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/8609607d-9bdd-494a-94bc-120fa686fe2d/
The interesting quote is from the MDSN article: “...snip...Arrays are layed out differently in managed code vs
unmanaged code, so it was a total fluke that it worked simply be passing the first element ByRef anyway.”
The correct syntax for BOTH ia32 and x64 should follow this example:
Module Module1
Private Declare Sub FortranDLL Lib "FCALL.dll" Alias "FORTRANDLL" (<[In](), Out()> ByVal
Array1() As Int32, ByRef Foo As Int32)
Sub Main()
Dim I As Int32
Try
Dim Test As Int32() = {1, 2, 3, 4, 5, 6}
FortranDLL(Test, Test.Length)
Console.WriteLine (" ")
Console.WriteLine ("back in VB")
Console.WriteLine ("Array elements after call return:")
For I = 0 To 5
Console.WriteLine (Test(I))
Next I
Catch ex As Exception
Console.WriteLine (ex.Message)
My.Computer.Clipboard.SetText (ex.Message)
Finally
Console.ReadKey()
End Try
End Sub
End Module
! FortranDLL
! Description: This sample demonstrates a mixed VB-Fortran
! x64 application. The purpose is to demonstrate
! the VB declarations necessary to pass an array
! from VB to IVF.
! This is a Fortran subroutine contained in a
! DLL Project type and produces a DLL to be called
! from Visual Basic.
! Input files:
! NONE
! Output files:
! NONE
! History: v1.0
Subroutine FortranDLL(Array1, upbound)
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL
!DEC$ ATTRIBUTES REFERENCE :: Array1
!DEC$ ATTRIBUTES REFERENCE :: upbound
Implicit NONE
! ...argument declarations
Integer(4) :: upbound
Integer(4) :: Array1(1:upbound)
! Local variables
Integer(4) :: I
write(*,'(A,I)') "Array upper bound passed in ", upbound
write(*,*) "Array elements passed in: "
do i=1,upbound
write(*,'("element:", I2, 2X, I8)' ) i, Array1(i)
end do
write(*,*) "Adding 10 to each element and passing back to VB"
Array1 = Array1 + 10
End Subroutine FortranDLL
Article Attachments
VB-Calls-Fortranx64.zip
I have downloaded the trial version of Intel's Visual Fortran Compiler and I am investigating the possibility of
creating a DLL with Fortran and using the DLL in Visual Basic. I am using Visual Studio 2005.
I began by opening a Fortran Dynamic Link Library type project template in Visual Studio. I have modified
the Subroutine to produce a trivial Fuction that returns a value. The Fortran code is:
Function FortranDLL
! Expose subroutine FortranDLL to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT::FortranDLL
FortranDLL = 3.141
Return
end
I then added a standard Windows Application to the project. I added the following Visual Basic Declare
statement to declare my test DLL and call the DLL from the Form's Click Event.
When I run the VB code above I receive the following error message stating that the program is unable to find
the EntryPoint for the DLL.
Can you point me to information that will help me create a simple Function in the form of a DLL with Intel's
Fortran Compiler and call that Function from Visual Basic using Visual Studio 2005? If it is relatively easy
to do I will purchase the Compilier for use in my programs.
Thank You.
Jim Denny
here are two additional things you have to do, both of which can be addressed by adding the following line to
the Fortran code:
First, Intel Visual Fortran converts all routine names to uppercase and (on IA-32), prefixes the routine name
with an underscore. So what is created is the symbol _FORTRANDLL which does not match what Visual
Basic is looking for.
The second problem is that there are two different conventions for calling routines on 32-bit Windows,
STDCALL and C. VB uses STD CALL and Intel Visual Fortran uses C. The line I suggested above selects
STDCALL. I added REFERENCE also in case you change the code to pass arguments, as STDCALL implies
pass by value.
You can get more information in the mixed-language programming chapter of the on-disk documentation.
--------
Steve
Thanks Steve.
That was a big help. Works like a charm. That got me pointed in the right direction. I know the Fortran
compiler will help significantly speed up intensive numerical analysis. It appears that developing DLL's with
Fortran for use in VB is fairly straight forward and might be a viable option for me. I think I can figure out
the rest from here. As they say, "I know just enough to be dangerous".
Thanks,
Jim
Other user :
his program dosn't go with this sttributes too.
I use this attribute
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
no precluding,sorry
where is the problem?
Another User
when I build your test program with the correct attributes, I get a DLL which contains a "FortranDLL"
routine. So the problem is somewhere else - perhaps your VB code is referring to a different DLL. I cannot
tell what is wrong from the information you have given us.
One thing you can do is to download the free tool Dependency Walker and use it to look at your DLL. Make
sure that it shows that FortranDLL is an exported function and that it does not show any errors.
Function FortranDLL(x,y)
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
real x
real y
FortranDLL = 3.141*x*y
Return
end
error mesage>Attempted to read or write protected memory. This is often an indication that other memory is
corrupt.
In the VB declaration of FortranDLL, change ByVal to ByRef. The Fortran default is to pass variables
by reference. If you had used STDCALL without REFERENCE, that would change the default to by
value, but adding REFERENCE changes it back.
--------
Steve
Hi Steve,
I've been following this thread, but no one seems
to be having the same trouble that I am. I'm
attempting a very simple procedure (shown here):
FUNCTION FORTRANCALL(R1,NUM)
!DEC$ATTRIBUTES DLLEXPORT:: FORTRANCALL
!DEC$ATTRIBUTES ALIAS: 'FORTRANCALL'::FORTRANCALL
!DEC$ATTRIBUTES STDCALL,REFERENCE:: FORTRANCALL
NUM = R1 - 25
End FUNCTION
Module Module1
Declare Sub FORTRANCALL Lib "C:FORTRANFCALLFCALL...
...ReleaseFCALL.dll" (ByRef R1 As Integer, ByRef NUM As Integer)
End Module
You definitely should make the Fortran code a subroutine as you're going to corrupt the floating point stack if
you don't.
..yet another reason for including IMPLICIT NONE in all one's routines etc.ç
__________
First, Intel Visual Fortran converts all routine names to uppercase and (on IA-32), prefixes the routine name
with an underscore. So what is created is the symbol _FORTRANDLL which does not match what Visual
Basic is looking for.
The second problem is that there are two different conventions for calling routines on 32-bit Windows,
STDCALL and C. VB uses STD CALL and Intel Visual Fortran uses C. The line I suggested above selects
STDCALL. I added REFERENCE also in case you change the code to pass arguments, as STDCALL implies
pass by value.
You can get more information in the mixed-language programming chapter of the on-disk documentation.
hi everybody:
i´ve got a questin related to this topyc. if the created fortran dll uses a numerical library (i have in the code:
use numerical_libraries), when i call the created dll from visual basic, it can´t find it in a pc where fortran is
not installed. that may be because it can´t find the numerical libraries. where and which files do i have to add
to this pc so that it can work properly.
any idea?
Thanks
As with any DLL library you link against, you need to have a copy of that DLL on the end-user system
in a place Windows knows where to find it. If you have specified that you are building against the DLL
version of IMSL, you will need the appropriate IMSL DLL (which is NOT named
"numerical_libraries"). I suggest that you use Dependency Walker to determine what the name of the
DLL is, as VB won't tell you. Please note that your application must use IMSL and not just provide
IMSL functionality to some other program,
FORTRAN DLL Overview
The concept of communicating between procedures written in more than one language, in this case
FORTRAN and VBA is alien to most designers.
There are many advantages however to communicating with procedures written in other languages.
The main reason is a programmer does not have to rewrite verified code
VBA communicates with FORTRAN procedures by compiling FORTRAN source files, object files, static
libraries...etc into what are called Dynamic Link Libraries or FORTRAN DLL’s for short.
A FORTRAN DLL resembles a FORTRAN static library; the main difference being the DLL needs an
interface specification and associates itself with a project during execution, not during linking.
VBA procedures call and execute FORTRAN procedures contained in a FORTRAN DLL.
Externally declared Subroutine and Function procedures inside a FORTRAN DLL are the only procedures
that can be called and executed by a VBA procedure.
The definition of external means the Subroutine or Function procedure is not defined within an another
program unit in the FORTRAN DLL.
A FORTRAN DLL shares its code and data address space with the application calling it.
The organizational advantage of a static library, which means it is not included in your VBA project’s
code.
This means the workbook / project is smaller and takes up less memory when open in Excel.
Object code inside the FORTRAN DLL is associated in a dynamic manner with the calling procedure
when executed.
This means if a DLL contains 400 procedures, only the object code needed for the current routines are
utilized.
For a VBA procedure to execute a FORTRAN DLL Subroutine or Function procedure, VBA must first
know:
What type of FORTRAN DLL procedure will be executed and the name of the procedure.
The FORTRAN DLL procedure’s dummy arguments and their data types.
The statement that declares this information to VBA is called a Declare statement.
FORTRAN DLL's can be run from both VBA Sub procedures and Function procedures. Which means you
can run a 10,000 line numerical analysis routine from a single worksheet cell. Think of the possibilities.
How VBA Procedures Communicate with a FORTRAN DLL
VBA procedures call and execute FORTRAN DLL Subroutine and Function procedures by Call
statements.
A central concept about this calling strategy is a VBA procedure can run a FORTRAN DLL procedure,
but a FORTRAN DLL procedure cannot in turn run a VBA procedure.
This strategy in mind, VBA procedures can be thought of as pre/post information processors for
FORTRAN DLL routines.
Excel in theory becomes the GUI dashboard that FORTRAN never had
VBA procedures know how to call a FORTRAN routine via a Declare statement. AN Example Declare
statement is presented below.
Declare Sub FinVel Lib “C:\Project\EngPrc.DLL” (m1 As Double, v1i As Double, m2 As Double, vf
As Double)
The name after Sub defines the procedure to call in FORTRAN, Lib is the path to the DLL and the ( )'s
are its argument list.
‘VBA Declare statement that identifies the FinVel SubRoutine in the EngPrc _ FORTRAN DLL.
Declare Sub FinVel Lib “C:\Project\EngPrc.DLL” (m1 As Double, v1i As Double, _
m2 As Double , vf As Double)
Sub Velocity( ) ‘Procedure gathers info for FinVel and outputs its calculations.
Dim m1 As Double, v1i As Double, m2 As Double, vf As Double
m1 = Range(“B2”).Value ‘Read m1 mass value (kg) from cell B2 on active worksheet.
v1i = Range(“B3”).Value ‘Read velocity of m1 (m/s) from cell B3 on active worksheet.
m2 = Range(“B4”).Value ‘Read m2 mass value (kg) from cell B4 on active worksheet.
Call FinVel(m1, v1i, m2, vf) ‘Call FORTRAN FinVel Subroutine procedure. Use arguments m1, v1i, m2, vf
to pass information to FORTRAN DLL procedure. Use vf to pass back answer.
Range(“B7”).Value = vf ‘Set cell B7 value on active worksheet to vf.
End Sub
http://www.nag.co.uk/numeric/csharpinfo.asp
Con titulo
NAG:
Introduction to the NAG DLLs
Many organizations integrate our software seamlessly into a variety of applications, including Visual Basic,
Visual Basic Applications, Excel, Microsoft C++, Borland C++, Borland Delphi, PowerBuilder, S-Plus,
LabView and Watcom C/C++, using NAG's dynamic link libraries. Users also access NAG algorithms from
MATLAB using the NAG Toolbox for MATLAB.
Developers know that by taking this approach they save development time and reduce maintenance
overheads. With NAG's global reputation for quality and performance they can focus on the specialist aspects
of their application and have confidence in the results.
Our software covers a broad range of numerical and statistical techniques, such as optimization, PDEs, ODEs,
FFTs, correlation and regression, and multivariate methods, to name but a few. List of numerical and
statistical routines available.
Companies rely on our software for mission-critical applications, so we operate very stringent quality
assurance standards. No algorithm enters our software until it has been fully tested, validated and verified.
Underpinning the quality of all NAG software is our renowned technical support. Customers have
comprehensive documentation, as well as access to the actual developers of the software to ensure that their
questions are answered to the highest standards.
Learn how easy it is to integrate NAG algorithms into your application through the following links:
here is a free Fortran Compiler for Fortran77 from the GNU Project:
For Windows: http://www.geocities.com/athens/olympus/5564/
For Unix: http://www.gnu.org/software/fortran/fortran.html
This is a brief guide that outlines how to write, compile and run a Fortran program under UNIX.
Write a Fortran 77 code and place it in a file with the suffix .f
The command f77 will compile the code. A workable syntax is f77 -o fileout.e filein.f, which puts the
executable version of the Fortran from "filein.f" into "fileout.e". The suffix ".e" is not necessary, but is useful
in distinguishing executable files from others. A better syntax is f77 -O4 -Bstatic -o fileout.e filein.f, which
optimizes the executable for faster running.
If your code uses NCARGraphics routines you should substitute the command ncargf77 for f77 above.
Type the name of your executable file and it will run.
The author thanks G. Geers for proofreading this guide.