- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a bunch of Fortran code that I would like to be able to call using VB without making an additional DLL file. I have a VB DLL project and a Fortran DLL project in the same solution but I can't get VB to recognize the Fortran functions. This is not that big of an application so I'd like to have just one file to distribute that will do it all. I have setup the project dependencies and build order in the solution options. I am using Intel compiler 11.1 build 065 in MS Visual Studio 2008 along with the VB, VC#, VC++ in MSVS 2008.
Specific Questions:
1) Can this be done without first making the old code a DLL andcalling it as a reference?
2) What type of project do I need to setup the Fortran code in prior to adding to the solution?
3) What commands/syntax is required to expose the functions to the VB code?
4) What commands/syntax is required to have VB recognize the Fortran functions
5) Can I make it so the Fortran code is exposed to be called directly as well (say from other Fortran code without going through the VB object interface)?
Specific Questions:
1) Can this be done without first making the old code a DLL andcalling it as a reference?
2) What type of project do I need to setup the Fortran code in prior to adding to the solution?
3) What commands/syntax is required to expose the functions to the VB code?
4) What commands/syntax is required to have VB recognize the Fortran functions
5) Can I make it so the Fortran code is exposed to be called directly as well (say from other Fortran code without going through the VB object interface)?
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You cannot avoid the Fortran dll.
here is an example:
A Fortran DLL project creates a DLL called "C:\F90\FDLLFROMVB\CALLDLLFROMVB\FCALL\Debug\Fcall.dll".
The contents of this DLL project is a single file:
Private Sub Command1_Click()
Dim r1 As Single
Dim Num As Single
r1 = Text1.Text
Call FortranCall(r1, Num)
Text1.Text = Str$(Num)
End Sub
where a command button Command1 reads a number from a text box Text1 on a VB form (see attached screen shot) and calls the Fortran routine to take the modulus and return the result to Text1.
Just substitute your own path to the Fortran DLL in the declaration and add the appropriate calls to the VB code.
Note that the ALIAS in the Fortran code produces a mixedcase 'FortranCall' symbol in the DLL for the default Fortran uppercase 'FORTRANCALL' routine name, so the mixedcase name used in teh VB code is recognised. Without the ALIAS, you would have to use uppercase names in the VB code to match the uppercase symbols that Fortran produces by default. Get the names to match and you are on your way. The DLLEXPORT compiler directive ensures that a symbol for the subroutine name is created in the export table that is created along with the .DLL file (the .LIB file)
here is an example:
A Fortran DLL project creates a DLL called "C:\F90\FDLLFROMVB\CALLDLLFROMVB\FCALL\Debug\Fcall.dll".
The contents of this DLL project is a single file:
[bash] ! Fortran Code establishing subroutine
! that Computes the MOD of R1 and 256.0 and stores the
! result in the argument NUM
SUBROUTINE FortranCall (r1, num)
! Specify that the routine name is to be made available to callers of the
! DLL and that the external name should not have any prefix or suffix
!DEC$ ATTRIBUTES DLLEXPORT :: FortranCall
!DEC$ ATTRIBUTES ALIAS:'FortranCall' :: FortranCall
REAL,INTENT(IN) :: r1 ! Input argument
REAL,INTENT(OUT) :: num
num = MOD (r1, 256.0)
END SUBROUTINE
[/bash]
[bash]A VB project calls this DLL using the following code:
Declare Sub FortranCall Lib "C:F90FDLLFROMVBCALLDLLFROMVBFCALLDebugFcall.dll" (r1 As Single, Num As Single)[/bash]
Private Sub Command1_Click()
Dim r1 As Single
Dim Num As Single
r1 = Text1.Text
Call FortranCall(r1, Num)
Text1.Text = Str$(Num)
End Sub
where a command button Command1 reads a number from a text box Text1 on a VB form (see attached screen shot) and calls the Fortran routine to take the modulus and return the result to Text1.
Just substitute your own path to the Fortran DLL in the declaration and add the appropriate calls to the VB code.
Note that the ALIAS in the Fortran code produces a mixedcase 'FortranCall' symbol in the DLL for the default Fortran uppercase 'FORTRANCALL' routine name, so the mixedcase name used in teh VB code is recognised. Without the ALIAS, you would have to use uppercase names in the VB code to match the uppercase symbols that Fortran produces by default. Get the names to match and you are on your way. The DLLEXPORT compiler directive ensures that a symbol for the subroutine name is created in the export table that is created along with the .DLL file (the .LIB file)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I completed what you described here but I am still having some problems. I get a BadImageFormatException raised. I am unsure on how to get this handled.
The Fortran code that I have is (as a test case):
FUNCTION TEST01(N)
!DEC$ ATTRIBUTES DLLEXPORT :: Test01 !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'Test01' :: Test01
IMPLICIT NONE
INTEGER(4) N
INTEGER(4) TEST01
N=5
TEST01=2
RETURN
END
FUNCTION TEST01
The VB code includes:
Declare Auto Function TEST01 Lib "C:\Projects\Test\Release\MatrixSolver.dll" (ByRef N As Integer) As Integer
The Fortran code that I have is (as a test case):
FUNCTION TEST01(N)
!DEC$ ATTRIBUTES DLLEXPORT :: Test01 !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'Test01' :: Test01
IMPLICIT NONE
INTEGER(4) N
INTEGER(4) TEST01
N=5
TEST01=2
RETURN
END
FUNCTION TEST01
The VB code includes:
Declare Auto Function TEST01 Lib "C:\Projects\Test\Release\MatrixSolver.dll" (ByRef N As Integer) As Integer
Public Function FTest() As Integer
TEST01(2)
End Function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you are running on a 64-bit system, you must build a 64-bit Fortran DLL and set the VB project's platform to X64 (not "Any CPU"). If you don't do this, you'll get that error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you. I am on a 64-bit Win XP machine. Now I am stumped on how to set my Intel compiler to 64-bit with compiler version and build as shown above. I see the window to set it, but the only options shown are the Intel 32-bit. I have navigated to it in MSVS2008 by using the Configuration Manager -> Platform pull-down menu, select "Edit" -> Edit Project Platforms window. The only option that shows up is Win32. Do I need to set this up in the project prior to importing into the solution?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In Configuration Manager, pull down Active Solution Platform and select New. Here you will be able to add an x64 configuration.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here is what I have so far that still isn't working:

When I try to set the Intel Compiler to x64 by selecting "Edit" I get only this:

When I try to set the Intel Compiler to x64 by selecting "Edit" I get only this:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Have you in fact installed the x64 Intel compiler? If you select Tools > Options > Intel Visual Fortran > Compiler, can you select both Win32 and x64 targets?
What happens when you select New from that dropdown? Edit won't do what you want.
What happens when you select New from that dropdown? Edit won't do what you want.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page