fortran visual basic strings

fortran visual basic strings

Hi everyone. I would appreciate if someone could give me the pointers or answer to the following:

I created a dll called xstr.dll by using fortran. Now i use VB 6.0 where i try to call the xstr.dll, which works for VB(single), CVF(real*4)... integers and boolean values. However when i try to pass strings to the xstr.dll i get nowhere. Sometimes i get memory could not be read and sometimes nothing happens. Here is my fortran subroutine inside the xstr.dll:

function xstr(a)
character*1 a(*)
c write(*,*) (a(i),i=1,6)
c pause
a(1) = 'b'
a(2) = 'y'
a(3) = 'e'
a(4) = char(0)
xstr = a(4)

Now here is the VB code that i use to call this subroutine:

Public Declare Function xstr Lib "c:dllsxstr.dll" Alias "_xstr@12" (ByVal c As String) As String

If you know where i could get some extra info on this it would be appreciated. Any suggestions or ideas would be appreciated as well, thanks.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In this case the explicit argument to xstr actually gives you two arguments as character variables are implemented with a "hidden" length, so for each character variable you have an integer passed by value following by the string variable passed by reference. You need to declare and call the routine in VB to reflect this or change the attributes of that argument from the Fortran side.


Thanks for the pointer but i am a bit confused, being that all of this is a totally new thing for me. Let me try to simplify things a bit:

In VB 6.0 i have the following code:

This code is in my form on click event of the button:

Dim c as String
Dim x as String

c$ = 'hello'
x$ = xstr(c$)

MsgBox x$

Now the following code is in the module on my VB 6.0 app:

Public Declare Function xstr Lib "c:dllsxstr.dll" Alias "_xstr@12" (ByRef c as String) As String

What if anything am i doing wrong here? Do i have to pass ByVal or ByRef in VB to fortran? Do i need the Alias "_xstr@n" or can i leave blank the number of bytes being passed, for example Alias "_xstr".

Now in my fortran code i have the following:

function xstr(a)
character*1 a(*)
xstr = a

What i would like is just to have a string passed to fortran code which is inside of the dll and have that same string return back or with a small change just so that i can see that the i went from VB to fortran and back. Since i am not experience with fortran sorry if my code is not clear. If you can give me any pointers or maybe an example of code that i can follow i would appreciate that, thank you in advance.


Hi (zemljak?),
If you intended that only as a test, don't begin with strings; they can be really tricky as you discovered.

In Fortran, strings are treated as fixed-length buffers of given length. They're described as starting address+length. When you pass a string as an argument to Fortran routine, it expects to find string's address and its length on the stack; thus, your xstr will be mangled by default as _XSTR@8.

However, in VB strings are quite a different beast - so-called BSTRs. BSTR consists of a starting word containing length, followed by an array of Unicode characters (2-bytes). See here for the full info.

A really good VB/Fortran mixed-programming site is Canaimasoft's f90VB page. See its User Manual -- apart from describing a specific product, it contains really good general info about F90/VB interaction.

I think I've seen workarounds for BSTR/Fortran string problem here or on the Old forum but I can't recall any at the moment (and I'm not very familiar with VB).

Happy New Year,

Message Edited by on 12-09-2005 02:12 PM

Declare your strings in VB something like:

Dim c As String * 64

or whatever to create a character array compatible with your Fortran function. See the RETURNSTRING sample provided with the compiler for a working example.


Leave a Comment

Please sign in to add a comment. Not a member? Join today