Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29271 Discussions

Passing derived types, vb.net and fortran dll

craig_h
Beginner
958 Views
I want to passderived types between a vb.net GUI and a Fortran dll. I would like the derived type to contain 1D and 2D arrays. Is any of this possible? I can't seem to find a complete and clear example of this and would appreciate some advice or if it's not possible to know for sure. Or know the limitations if some of it is possible but maybe not the arrays.

If derived types aren't possible what about 2D arrays not in a derived type? I've read where people have had trouble with this but again can't find a complete and clear example of it working.

Thanks
0 Kudos
4 Replies
Steven_L_Intel1
Employee
958 Views
I assume that VB derived types with arrays would have pointers to a SafeArray structure. This is not something we have looked at. There are two examples of VB.Net calling Fortran, one of which deals with SafeArrays as a routine argument. I suggest you look at this. These examples are in MixedLanguage.zip.
0 Kudos
craig_h
Beginner
958 Views
Steve
The example that deals with safearray structures passes an array of strings. If I want to pass an array of reals or integers would I stilluse asafearray along withthe BSTRPtr and ConvertBSTRToString function? How wouldthe methodologydiffer? How would I convert it to a real? Is there an easier more direct way of doing this?

Thanks
0 Kudos
Steven_L_Intel1
Employee
958 Views
You wouldn't use the BSTR stuff with reals or integers. So where the code calls SafeArrayGetElement, the pointer is to a REAL (or INTEGER or whatever) not a BSTR. So instead of:

integer(int_ptr_kind()) :: BSTRPTR

you might have:

real :: real_element
pointer (p_real, real_element)

and then pass loc(p_real) as the last argument to SafeArrayGetElement. You could then use variable real_element.

There might be a shortcut to access a section of an array but I'd have to play with it to see.
0 Kudos
anthonyrichards
New Contributor III
958 Views
I have no experience of derived types in VB .NET, but it is straightforward to pass arrays from VB .NET to Fortran without having to resort to SafeArrays. I attach sample code for the Visual Basic side to pass 2D arrays of Doubles, Integers and Longs and and the Fortran code required to receive them on the Fortran side.

All you need to remember this time is that VB .NET stores array elements in row-major order (as opposed to VBA in Excel which appears to store arrays in column-major order as per Fortran!).

The VB .NET code sets array value (i,j) = the decimal number 'ij' so that you can immediately see on the Fortran side the array indexes that VB .NET used for a received array value (as you can see in the Debug Screen-shot also attached).

P.S. I used Microsoft Visual Basic 2008 Express Edition which uses the .NET Framework
0 Kudos
Reply