Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29249 Discussions

Calling a Fortran created DLL Function from Visual Basic

bridgeguy
Beginner
4,940 Views

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 FortranDynamic Link Library typeprojecttemplate in Visual Studio. I have modified the Subroutine toproduce atrivial 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.

Public

Class Form1

Declare Auto Function TestDLL Lib "C:VB .NET ProjectsFotran DLLFortran DLLDebugfortran dll.dll" Alias "FortranDLL" () As Single

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

Dim Test As Object

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 DLLwith 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

0 Kudos
24 Replies
gib
New Contributor II
976 Views
Quoting - johnldarby

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 hoursobtaining a lot of bad advice before I found this excellent, concise advice.
I'll bet there's nowhere on the web where you'll get programming advice as good as what you get here. :-)
0 Kudos
dinksy
Beginner
976 Views
I am trying something similar...the fortran code is

Function DLL2(x)
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"DLL2" :: DLL2
real x
DLL2 = x
Return
end

and the visual basic code is

Public Class Form1

Declare Auto Function DLL2 Lib "D:\Abhinav\Visual Basic Codes\Dll2\Dll2\Debug\DLL2.dll" Alias "DLL2" (ByRef x) As Single

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Test As Single

Dim a As Single = 4.2

Test = DLL2(a)

MsgBox(Test)

End Sub

End Class

The program runs butprints 5.605194E-45. I cant figure out is that i am doing wrong. Kindly advise

0 Kudos
anthonyrichards
New Contributor III
976 Views
I put this code into a Dll:

Real*4 Function DLL2(x)
!DEC$ ATTRIBUTES DLLEXPORT,REFERENCE,ALIAS:"DLL2" :: DLL2
real*4 x
DLL2 = x
Return
end

Then I inserted some Visual Basic code into an Excel worksheet and tried it out:

Public Declare Function DLL2 Lib "P:\whateverpath\dll2_test\debug\dll2_test.dll" (A1 As Single) As Single


Public Function GetDLL2(a As Single) As Single
GetDLL2 = DLL2(a)
MsgBox (Str(a))
End Function

and I invoked GetDLL2 in a formula in a cell (e.g. '=GetDLL2(B39)' taking a value in cell B39) and it worked OK. The message box showed the value and the value turned up in the Excel cell containing the formula.
Note that I use MsgBox(Str(a)) in order to get the correct value displayed.

It even works with

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"DLL2" :: DLL2
as the export directive.

0 Kudos
Steven_L_Intel1
Employee
976 Views
You need the STDCALL, otherwise you'll corrupt the stack. It may seem to work in a small test case, but will cause strange failures in a real program.
0 Kudos
Reply