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

array-valued function with VBA

aid-usman
Beginner
517 Views

I have some array-valued function in a fortran dll and I like to use this function in VBA.

function func(n) result (y)

implicit none

integer(4) :: n

real(8), dimension(n) :: y

..

..

end function func

Is it possible to call/declare such an array-valued function and does someone know how to do this.

Thanks

Aid

0 Kudos
3 Replies
anthonyrichards
New Contributor III
517 Views

Try the following

VBA

Public Declare Functionfunc Lib "C:Winntsystem32yourfortran.dll"
(A1 As integer) As double

FORTRAN (note:Fortran makes everything UPPERCASE by default, so the ALIAS attribute is required tosupply a lowercaseentry 'func' in the Dll's exported symbol table that VBA needs, otherwise you need to make 'func' UPPERCASE in the VBA 'Public Declare' statement.]

REAL*8 FUNCTIONFUNC (N )
!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:'func' ::FUNC
INTEGER(4) N
.......
.......
FUNC=
....
END FUNCTION FUNC

0 Kudos
anthonyrichards
New Contributor III
517 Views

I have just realised that the above will not work for ARRAYs as Visual Basic uses 'Safe arrays', which are structures that are not easily represented in Fortran.

As Steve mentions below, you could examine the SAFEARRAY sample code which shows you how to convert a 2 dimensional Fortran array into a safearray. After you have generated your Fortran array of data, you could then call this code to generate a safearray from it, whose address you can then return to the calling VB program as Steve suggests, using a SUBROUTINE call rather than a FUNCTION call.

0 Kudos
Steven_L_Intel1
Employee
517 Views
This is harder than you think. A Fortran function returning an array looks like a subroutine with a "hidden" first argument that is the array return location. I don't think you can map this onto a VB function call returning an array. If you wanted it to look like a VB array function, you'll probably have to locate the SAFEARRAY descriptor that VB passes and fill it in. You can see an example of SAFEARRAY code in the MixedLanguageVB.NET-Safearray sample.

It would be easier if you called this from VB as a subroutine passing the first element of the array "return" as the first argument.
0 Kudos
Reply