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

Calling a Fortran created DLL Function from Visual Basic

bridgeguy
Beginner
2,757 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
Steven_L_Intel1
Employee
2,237 Views

There are two additional things you have to do, both of which can be addressed by adding the following line to the Fortran code:

!DEC$ ATTRIBUTES STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL

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.

0 Kudos
bridgeguy
Beginner
2,237 Views

Thanks Steve.

That was a big help.Works like a charm.Thatgot me pointed in the right direction. I know the Fortran compiler will help significantly speed upintensive 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

0 Kudos
nlbcostello
Beginner
2,237 Views

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

0 Kudos
Steven_L_Intel1
Employee
2,237 Views
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.
0 Kudos
nlbcostello
Beginner
2,237 Views

Tanks Steve,

I am trying to find and run a test program in the latest version of Intel's FORTRAN for Windows that I downloaded and installed on a test machine.

I am working within Visual Studio 5. All I want to do is show myself that I can write a FORTRAN dll and then call it from a FORTRAN console application.

In addition I want the projects to be seperate projects within the visual studiosolution.

I will also need to call fortran dlls from vb and call vb COM dll's created in managed code from fortran.

Thanks again,

Norm

0 Kudos
Steven_L_Intel1
Employee
2,237 Views

Norm,

I replied in the other thread you started.

0 Kudos
krecmer
Beginner
2,237 Views
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

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


Can you help me with this code?
I have fortran subroutines and functions and I need to make a visual environment in visual basic. I need to call a subroutin or function from basic with input arguments. An after calculation the fortran must return resuts to basic.
Your tablet code is it what I need. But this code is with error. could you write a corect code for fortran and basic.

Thanks

0 Kudos
Steven_L_Intel1
Employee
2,237 Views

You are missing the DLLEXPORT attribute in your Fortran code. Use this:

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL

0 Kudos
krecmer
Beginner
2,237 Views
this 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?
0 Kudos
Steven_L_Intel1
Employee
2,237 Views

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.

0 Kudos
krecmer
Beginner
2,237 Views

thanks for your help STEVE,
this code is operating.
But now I'm solving problem usin more input and output arguments in functions and subroutines. And these atrubutes is un know for me :(

Function FortranDLL(x,y)
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL
real x
real y
FortranDLL = 3.141*x*y
Return
end

and basic code>

Public Class Form1

Declare Auto Function FortranDLL Lib "D:_dokumentsVisual Studio 2005ProjectsWindowsApplication2Dll1Debugdll1.dll" Alias "FortranDLL" (ByVal x, ByVal y) As Single

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Test As Single
Dim b As Single = 4.2
Dim c As Single = 5
Test = FortranDLL(b, c)
MsgBox(Test)
End Sub
End Class

error mesage>Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
0 Kudos
Steven_L_Intel1
Employee
2,237 Views
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.
0 Kudos
jdchambless1
Beginner
2,237 Views

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):

Here's the Fortran Function:

FUNCTION

FORTRANCALL(R1,NUM)

!DEC$ATTRIBUTES DLLEXPORT:: FORTRANCALL

!DEC$ATTRIBUTES ALIAS: 'FORTRANCALL'::FORTRANCALL

!DEC$ATTRIBUTES STDCALL,REFERENCE:: FORTRANCALL

NUM = R1 - 25

End FUNCTION

Here's the VB Module Code:

Module

Module1

Declare Sub FORTRANCALL Lib "C:FORTRANFCALLFCALL...

...ReleaseFCALL.dll" (ByRef R1 As Integer, ByRef NUM As Integer)

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

Dim NUM As Integer

R1 = 75

NUM = 20

TextBox2.Text = Str$(R1)

TextBox3.Text = Str$(NUM)

Call FORTRANCALL(R1, NUM)

TextBox1.Text = Str(NUM)

TextBox4.Text = Str$(R1)

TextBox5.Text = Str$(NUM)

End Sub

The Fortran function is compiled as a DLL and referenced

by the VB program.

The program runs to completion, only the value for

NUM is shown as -25, instead of 50 (R1 - 25 = 50).

I'd appreciate it if you could spot any code blunders

for me, as I can't seem to figure it out.

Thanks.

0 Kudos
anthonyrichards
New Contributor III
2,237 Views
You are CALLing a FUNCTION? I would define your FUNCTION as a SUBROUTINE.
0 Kudos
Steven_L_Intel1
Employee
2,237 Views
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.
0 Kudos
anthonyrichards
New Contributor III
2,237 Views
..yet another reason for including IMPLICIT NONE in all one's routines etc.
0 Kudos
ixi-pixi
Beginner
2,237 Views

There are two additional things you have to do, both of which can be addressed by adding the following line to the Fortran code:

!DEC$ ATTRIBUTES STDCALL,REFERENCE,ALIAS:"FortranDLL" :: FortranDLL

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:

ive 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 cant find it in a pc where fortran is not installed. that may be because it cant find the numerical libraries. where and which files do i have to add to this pc so that it can work properly.

ive found numerical_libraries.mod and i add it but it doesnt work

any idea?

thanks

0 Kudos
Steven_L_Intel1
Employee
2,237 Views

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,

0 Kudos
martin_bauer
Beginner
2,237 Views
Hallo, Steve,

I use the sample program as list above:

Public Class Form1
Declare Auto Function TestDLL Lib "C:_JIANGCodeT1Console1Console1F.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

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)"

what is the wrong?

regards,

0 Kudos
johnldarby
Beginner
1,952 Views
Quoting - bridgeguy

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::FortranDLLFortranDLL = 3.141

Returnend

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




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.
0 Kudos
Reply