contents   index   previous   next



Passing strings by reference

 

The preferred method to pass Visual Basic string arguments to Fortran procedures that use f90VB is by reference [20]. When you pass a string by reference to a subroutine, the subroutine owns the string. This means the subroutine can change the string, delete it, or do whatever else it wants with it.

 

Explaining how to pass Visual Basic strings to your Fortran subroutines is best done through an example.

 

 

 

 

 

Example 4.2

 

In the following example, you create a Fortran subroutine (DuplicateStr) that uses f90VB's BString procedures to expand a string passed as an argument by appending a copy of itself to the right. As in the previous example, for simplicity the Fortran code shown does not include any of the compiler-dependent commands necessary to create the DLL for this subroutine, or to indicate the calling convention. These are explained in the next chapter.

 

This is the Fortran code for subroutine DuplicateStr:

 

subroutine DuplicateStr(BStrHndl)

 

    use f90VBDefs

    use f90VBBstrings

    implicit none

 

    integer(BSTRHNDL_KIND),intent(inout)::BstrHndl

    

    !Convert from VB External BString format to standard BString

    call BStrConvFromVB(BStrHndl,BStrHndl)

 

    !Append a copy of the string to itself

    call StrConcat(BstrHndl,BstrHndl)

 

    !Convert back to VB External BString format

    call BStrConvToVB(BStrHndl)

 

end subroutine DuplicateStr

 

 

Note that DuplicateStr receives a single argument, which is a handle to a BString. The argument is declared with intent(inout), because the procedure modifies the passed BString. Although it is not always the case, when you modify a BString its handle might change (i.e. the BString might be reallocated to a different memory position). DuplicateStr calls f90VB's subroutine BStrConvFromVB because we plan to call this subroutine from a Visual Basic program, and Visual Basic passes BStrings to external procedures with a special format (see Chapter 1). Subroutine BStrConvFromVB converts the passed BString from Visual Basic's external format to the standard BString format used by other f90VB procedures. DuplicateStr then calls subroutine StrConcat, which concatenates the BString passed as the second argument, to the end of the BString passed as the first argument. Before the concatenated string is returned to Visual Basic, it must be converted to the special BString format Visual Basic expects for BStrings returned by external subroutines. This is accomplished by calling f90VB's subroutine BstrConvToVB.

 

Using your Fortran compiler, you can export the subroutine into Example42.dll (the next chapter explains in detail how to do this).

 

To call DuplicateStr from Visual Basic or Visual Basic for Applications, you need to declare the subroutine as an external procedure, and give VB the location of the DLL that contains the procedure. To see how this is done, you can create a new Visual Basic project, and add a module file with the following declaration:

 

Declare Sub DuplicateStr Lib "Example42.dll" (Str As String)

 

As in the previous example, we assume that Example42.dll and the visual basic executable reside in the same directory. You can now add a simple form to request an input string and print the resulting string (Figure 4.2).

 

To call subroutine DuplicateStr, add the following code to the Click event of the Go! button:

 

Private Sub cmdGo_Click()

 

    Dim Str As String

    Str = txtInputStr.Text

    Call DuplicateStr(Str)

    txtOutputStr.Text = Str

 

End Sub

 

Figure 4.3 shows an example of the result after entering an input string and clicking the Go! Button.

 

Passing strings by value