Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26736 Discussions

Passing array of characters from Fortran DLL to VBA

Lorenzo_W_
Beginner
223 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
Black Belt
223 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

5 Replies
Steve_Lionel
Black Belt Retired Employee
223 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.

Lorenzo_W_
Beginner
223 Views

Thank you for the hint.

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

FortranFan
Honored Contributor II
223 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?

DavidWhite
Black Belt
224 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

Lorenzo_W_
Beginner
223 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

Reply