Passing arrays from Visual BASIC 6.0 to a CVF dll.

Passing arrays from Visual BASIC 6.0 to a CVF dll.

What is the procedure??? I have implemented the example in the CVF manual exactly but I continue to have garbage passed across the interface. The manual says to pass the first element of the array and the dimensions of the array and then, in the FORTRAN dll, simply declare the array of the proper size. Doesn't work. Has anybody figured this one out.

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

Yes, people have figured this (and much more) out.

For best results getting help with this, post the code causing the difficulty and someone will point out the error. Include the f90 routine declaration, the VB Declare statement, and the VB calling code.

-John

Visual basic arrays are a type of array called safe arrays. They are not directly compatible with fort ran arrays. If all you need is to read/write from/to the elements in the array, the easiest way to do it is pass the first element of the array:

dim I as long, I as long
dim my array(10,10) as single
I=10
I=10
...
call mydllsub(my array(1,1),I,I)

the declare statement should look like (on the assumption your Fortran dll is on windows? system directory):

Declare Sub mydllsub lib "c:winntsystem32mydll.dll" (byref firstelement as single, byref n as long, byref m as long)

where n and m are the dimensions of the array. On the fortran side, declare your argument as:

subroutine mydllsub(myarray,n,m)

!DEC$ATTRIBUTES STDCALL:: mydllsub
!DEC$ATTRIBUTES ALIAS: 'mydllsub':: mydllsub
!DEC$ ATTRIBUTES reference :: myarray,n,m

integer(4)::n,m
real(4)::myarray(n,m)

This should do the trick. If you need to do anything more complicated than just read/write the elements in your fortran sub, or if the elements are of a non-fortran-compatible type, then you have to deal with the safe array itself, which is not quite as easy. You can learn more about this by checking MSDN documentation on safe arrays. If I remember right, one of the CVF newsletters also has some explanations and examples on safe arrays.

Regards,

Marco A. Garcia
Canaima Software
P.O. Box 13162
La Jolla, CA. 92039
U.S.A.
e-mail:mgarcia@canaimasoft.com
Tel/Fax: (619) 233-6831
http://www.canaimasoft.com
Developers of f90SQL the Database Connectivity Solution for Fortran, and f90VB the Library for Fortran-OLE Automation and Fortran-VB Programming

Thanks for the info. Turned out to be a declaration problem. The default type in VB is Integer(2). Apparently the default type in Fortran is *4. I was declaring the types in both pieces of code as "Integer". When I changed the VB declaration to "Long", the problems disappeared. The manual is a little misleading on this one but I should have caught it sooner.

Do VB multidimensional arrays have column major order like Fortran arrays do, or do they have row major order?

I have an Excel VBA routine that calls a Fortran DLL, passing several 2-d integer and floating point numeric arrays by specifying their first elements in the Call statement. I don't do anything to transpose the arrays first, nor do I switch the indices around in my Fortran declarations of the arrays. That leads me to believe that both VBA and Fortran use column major order; however I seem to remember reading somewhere that VB and VBA use row major order (although I can't remember where).

Mike Durisin

Column-major as in Fortran

regards,

Marco Garcia
Canaima Software
www.canaimasoft.com

Leave a Comment

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