Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
11 Views

Passing array of characters from Fortran DLL to VBA

Jump to solution

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

Accepted Solutions
Highlighted
Black Belt
11 Views

For simple cases passing

Jump to solution

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
Highlighted
Black Belt
11 Views

There is a complete example

Jump to solution

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.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Beginner
11 Views

Thank you for the hint.

Jump to solution

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
Highlighted
Valued Contributor III
11 Views

Quote:Lorenzo W. wrote:

Jump to solution

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
Highlighted
Black Belt
12 Views

For simple cases passing

Jump to solution

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
Highlighted
Beginner
11 Views

Dear all,I think that I can

Jump to solution

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