![]() |
|
|||||||||||||||||||||||
|
|
Passing BString arguments from Visual Basic to Fortran DLLs (Example4.2) In the following example, we 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. 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 User Manual of f90VB. 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 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 (this is explained in details in f90VB's User manual). 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:
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
The following screen shows an example of the result after entering an input string and clicking the Go! button:
|
|||||||||||||||||||||||
|
Copyright
© 1998-2000 Canaima Software
For questions regarding this site, send an e-mail to webmaster@canaimasoft.com |
||||||||||||||||||||||||