Calling fortran from Visual Basic

Calling fortran from Visual Basic

I'm trying to pass a structure from VB to fortran like "
type aaa
val1 as integer
car as string
str (1 to 5) as string
end type

dopes anyone know how to do that or any website that has a sample ?

Thanks

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

This is actually an interesting, if tedious, example of the special cases of passing UDTs to Fortran from VB.
OK, here we go.

Note that because the UDT contains a string element, VB will create a copy of the UDT that contains an ANSI BSTR element and pass that instead of the real address of the UDT which contains UNICODE BSTRs. Basically, VB is doing copy-in/copy-out. The nice side of this is that you don't have to do the conversion from UNICODE yourself on the Fortran side of things.

The first element of the VB UDT is "val1 as Integer". This is a 2 byte integer (INTEGER*2). Note that VB pads UDTs to four byte boundaries, so you must account for that when you create the corresponding f90 TYPE on the Fortran side of things. Also, remember to use the SEQUENCE keyword.

The second element of the VB UDT is "car as string". Note that the passed UDT has a pointer to an ANSI BSTR here. So the f90 TYPE should contain a 4-byte integer here to hold the ANSI BSTR.

The third element of the VB UDT is "str (1 to 5) as string". OK. This is a good one too. This is a SafeArray of BSTRs. Of course, as mentioned below, the copy of the UDT that is passed on copy-in will be a SafeArray of ANSI BSTRs. But since this is a fixed size array in a UDT, the SafeArrayDescriptor is not passed at all, instead the SafeArray data is passed inline in the UDT. So what will be passed is 5 pointers (in a row in memory) to ANSI BSTRs. So an array of five 4-byte integers should do the trick here, inside the f90 type.

Other notes. You can use integer pointers to access the ANSI BSTRs inside your Fortran code. Be careful of whether you intend to modify the string elements of the UDT in your Fortran code. If you must modify the strings the dangerous way is to modify NO MORE than the length of the BSTRs (found by examining the 4 byte integer passed just prior to the address of the ANSI BSTR - or use SysStringByteLen on the BSTR). The correct way to modify the strings is through the use of the functions SysStringByteLen, SysAllocStringByteLen, and SysFreeString. See the CVF Mixed Lang VB programming sample ARRAYS for more info on this if you wish.

If you want to see an example of your query, check out:
VB form - http://www.termine.org/f90/VB_UDT/VB_UDTwithStrArr.html
CVF dll routine - http://www.termine.org/f90/VB_UDT/UDT_Sample.html

After all this you may decide not to pass this structure, but it's up to you. ;-)

hth,
John

Canaimasoft's f90VB user manual is a quite worthwhile reference.

Jugoslav
www.xeffort.com

Leave a Comment

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