contents   index   previous   next



Strings as arguments to external procedures

 

Most Windows API functions are not able to handle BStrings. As BString is the intrinsic string type in Visual Basic, this would have created a major problem when calling Windows services from Visual Basic. To maintain compatibility with automation-unaware Windows services, Visual Basic treats string arguments to external subroutines (i.e. those residing in a DLL and defined as external using Visual Basic's Declare statement) in a different way [03].

 

Before passing a string argument to a procedure that has been defined with a Declare statement, Visual Basic converts the argument into a special type of BString. This special BString has a standard header, and is also null-terminated, but each character uses only one byte instead of Unicode's two-byte standard. Because the handle to a BString is a pointer to its first character, Windows API functions that expect a C-style string as an input argument will work with these special BStrings. When the external procedure returns control, Visual Basic will convert the special BString back into a standard BString for internal use. If the external procedure changes the BString argument, it must return a BString in this special format for Visual Basic to process the changes correctly [04]. These operations are illustrated in Figure 1.8.

 

When you call Fortran procedures residing in a DLL, Visual Basic will perform the operations illustrated in Figure 1.8. Most string functions provided in f90VB work only with standard BStrings, however, f90VB offers two functions that will help to interface with Visual Basic. Subroutine BStrConvFromVB takes a Visual Basic BString, as it is passed to an external procedure, and converts it into a standard BString. Subroutine BStrConvToVB will convert a standard BString into the special format that Visual Basic expects to receive from external procedures. In general, the first thing you should do in a Fortran procedure that receives BString arguments from Visual Basic is to call BStrConvFromVB on the arguments to convert them into standard BStrings. If the Fortran procedure is returning BStrings back to Visual Basic, it should call BstrConvToVB just before returning, so the BStrings can be correctly interpreted by Visual Basic. See the examples in the following chapters for more details.