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

Passing array of characters from Fortran DLL to VBA

Lorenzo_W_
Beginner
834 Views

I am trying to link a Fortran DLL to VBA.

I started by using Intel example (https://software.intel.com/en-us/node/611723) and I am able to deal with all kind of data except array of strings.

Here is my example:

FORTRAN:

SUBROUTINE TESTARRAY (ELEMENTS, OUTARRAY) ! bind(c, name='TESTARRAY')
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, REFERENCE, ALIAS:"TESTARRAY" :: TESTARRAY

IMPLICIT NONE

INTEGER*4, INTENT(IN) :: ELEMENTS
CHARACTER*15, INTENT(OUT) :: OUTARRAY(ELEMENTS)
INTEGER*4:: I
!DEC$ ATTRIBUTES REFERENCE :: ELEMENTS, OUTARRAY

DO I = 1,ELEMENTS
    WRITE(OUTARRAY(I),'(I3.3)') I
ENDDO

RETURN

END SUBROUTINE TESTARRAY

------------------

VBA:

 

Option Explicit
Option Base 1

Dim arraytest() As String * 15

Declare Sub TESTARRAY Lib "PATH\MYDLL.dll" (ELEMENTS As Long, ByVal OUTARRAY As String)

Public Function array_dll(iel As Long) As String

   ReDim arraytest(1 To iel)

    Call TESTARRAY(iel, arraytest(1))

    array_dll = Trim(arraytest(iel))

End Function

 

When I try to use inside an Excel sheet the function "array_dll" linked to a cell containing for example "5", I obtain an empty cell, and not "005" as expected.

How is the right method for dealing with that kind of data?

Thank you!

0 Kudos
1 Solution
DavidWhite
Valued Contributor II
834 Views

For simple cases passing multiple strings (not just arrays), I have sometimes just concatenated the strings on the VBA side and then unpacked them on the DLL side. (and of course, the reverse for passing the strings back).

View solution in original post

0 Kudos
5 Replies
Steve_Lionel
Honored Contributor III
834 Views

There is a complete example VB.NET-Safearrays in the Intel sample bundle that demonstrates doing exactly this. It's a lot more complicated than what you show above.

0 Kudos
Lorenzo_W_
Beginner
834 Views

Thank you for the hint.

It is a lot more complicated than I thought, maybe I should try a workaround.
Thank you again

0 Kudos
FortranFan
Honored Contributor II
834 Views

Lorenzo W. wrote:

.. It is a lot more complicated than I thought, maybe I should try a workaround ..

@Lorenzo W.,

How is the actual argument (i.e., the object equivalent to arraytest in your example above) used in the VBA code?  Specifically, does it have to be an array?

0 Kudos
DavidWhite
Valued Contributor II
835 Views

For simple cases passing multiple strings (not just arrays), I have sometimes just concatenated the strings on the VBA side and then unpacked them on the DLL side. (and of course, the reverse for passing the strings back).

0 Kudos
Lorenzo_W_
Beginner
834 Views

Dear all,
I think that I can re-think about my output.

As David White suggested me, the best think I can do is to concatenate and then unpack strings, it is more simple than all other solutions.

 

Thank you all

0 Kudos
Reply