综合讨论版 #9981 层

Visual Basic - Visual Fortran DLL Example

楼主 · 1 楼
The Programmer's Guide FortranCall example of calling a Fortran routine in a DLL from Visual Basic has some errors. The following steps show how to create a working example that also shows how to pass string arguments.

Note: This example has been tested using Visual Basic 5.0. It may not work using earlier versions of Visual Basic.

In Developer Studio, Create a new project of type Win32 Dynamic-Link Library, call it FCALL.

Create a new free-form source file FCALL.F90 with the following code:

! Fortran part of a VB-Fortran DLL example. This
! routine DLL_ROUT is called from a VB executable
! program.
SUBROUTINE DLL_ROUT (INT_ARG, STR_IN, STR_OUT)
IMPLICIT NONE

! Specify that DLL_ROUT is exported to a DLL
! and that the external name is 'DLL_ROUT'
!DEC$ ATTRIBUTES DLLEXPORT :: DLL_ROUT
!DEC$ ATTRIBUTES ALIAS:'DLL_ROUT' :: DLL_ROUT

INTEGER INT_ARG
CHARACTER*(*) STR_IN, STR_OUT

! This routine converts INT_ARG to a decimal string.
! appends the string value to STR_IN and stores it
! in STR_OUT.
!
! Note that there are implicit length arguments
! following the addresses of each CHARACTER argument.

CHARACTER*5 INT_STR

WRITE (INT_STR,'(I5.5)')INT_ARG

STR_OUT = STR_IN // INT_STR

RETURN
END

Build the DLL.

Start Visual Basic and create a new Standard EXE project. On the control toolbar, click on the CommandButton icon and then, with the cursor over the form, draw out a button. In the button's Properties box, double-click on Caption and change the caption to "Do it!". Click on the TextBox icon and, in the same fashion, draw a text box on the form. In its Properties box, find the Text property and change it to an empty string.

Double-click on the Command1 button on the form - a code window will appear. Fill in the code so that it looks like this:

Private Sub Command1_Click()

Static STR_IN As String * 10, STR_OUT As String * 20
INT_ARG = 123
STR_IN = "Testing..."

rem Pass lengths of string arguments

Call DLL_ROUT(INT_ARG, STR_IN, Len(STR_IN), STR_OUT, Len(STR_OUT))

MsgBox (STR_OUT)

End Sub

Select Project..Add Module and click on Open to create a new module. Add the following code to the module (all of it goes on one line - broken into multiple lines here for clarity):

Declare Sub DLL_ROUT Lib "c:\Program Files\DevStudio\
MyProjects\Fcall\Debug\Fcall.dll" (INT_ARG As Long, ByVal STR_IN As String, ByVal STR_IN_LEN As Long, ByVal STR_OUT As String, ByVal STR_OUT_LEN As Long)

Replace the filename with the location of the Fortran DLL, if different.

Run the Basic program by pressing F5. Click on the Do it! button. The Fortran routine will be called to convert INT_ARG to a string, appending that string to STR_IN and storing the result in STR_OUT. The Basic code will then display the result in the text box.
Notes: Visual Basic, like Fortran, passes numeric values (such as integers and reals) by reference, therefore it is not necessary to change the passing mechanism on either side. The ALIAS attribute is required because Visual Basic, even though it uses the STDCALL calling mechanism, does not "decorate" routine names with the @n suffix. If the Fortran routine were also to be called by other Fortran code, it would be appropriate to use the Alias option on the Basic side to name it with the proper suffix.

When passing strings, you must use ByVal on the Visual Basic side. By default, Visual Basic passes strings as an OLE "BSTRING" type which is not directly usable by Fortran. Specifying ByVal forces Visual Basic to simply pass the address of an ASCII version of the string. DIGITAL Visual Fortran, by default, expects CHARACTER arguments to be passed by address with a separate "hidden" integer length argument, passed by value, following the address. This is why the example above explicitly passes the lengths of the string arguments.