Passing array of strings from VBA to fortran

Passing array of strings from VBA to fortran

Hi,

How to pass of array of strings from VBA to fortran dll.

1) I am able to pass a single string by value with a length of string.

2) I am also able to pass a integer and real array between VBA and fortran dll.

3) However when I try to pass array of strings its only passes the 1 st element.

Below is simplified version of my code.

!--------------------------------------------------------------------------------------------------------------------------------

Fortran Subroutine

SUBROUTINE

trial(b)

!DEC$ ATTRIBUTES DLLEXPORT, ALIAS : 'trial' :: trial

IMPLICIT NONE

CHARACTER (LEN = *):: b(1:2)

b(1) = "TECH"

b(2) = "TEST"

END SUBROUTINE trial

!---------------------------------------------------------------------------------------------

VBA Subroutine

Option Explicit
Option Base 1

Declare Sub trial Lib "C:\ScrewCompressor\SDT\Fortran\proflib - TRIAL\Dll2\dll_run\Debug\Dll2.dll" (ByVal b As String, ByVal blen As Long)

Sub trial123()

Dim b(1 To 2) As String * 4

Dim N As Long

b(1) = "TECH"

b(2) = "TEST"

Call trial(b(1), Len(b(1)))

End Sub

 Thanks,

Amit

publicaciones de 8 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

Change ByVal to ByRef and add a REFERENCE attribute to the string array in the Fortran code. I would also terminate your Fortran strings with CHAR(0).

The VBA crashes when BY ref is added in DLL declaration.

Why are you passing the scalar, B(1),  from the VBA code when the Fortran routine is expecting an array?

For consistency, I would expect that you should pass the whole array.

Also, I would try setting the character array on the fortran side to LEN=4 instead of LEN=* - that has been more successful for my routines, although I have not passed character arrays.  The other thing that has helped, is to pad the character variables on the VBA side with blanks before calling.

Hope this helps.

David

 

I think you have a conceptual issue that you need to think about first.

- An array of strings in Fortran is rectangular - each element of the string has the same length.  The significant length of an element is indicated by the last non-blank character.

- An array of strings in VBA is not - each element can have different lengths.

How to do this depends on which "array of strings" approach you want.

The first is easy in terms of the level of language knowledge required, but requires some administrative overhead (you create a big string on the VBA side that has the values of the individual elements of the array concatenated together with appropriate blank padding, pass that one big string to Fortran, work on it, then "undo" the concatenation back on the VBA side).

The second (from memory) involves working with safe arrays and BSTR's.  You might not use a classic Fortran array of strings at all in this case.

Which array of strings approach do you need?

David,

Arrays are passed from VBA to Fortran using pointer to first element. It works perfect for Integers and Real but it does nott work for strings. I have tried it both ways and also by reference and by values but it still does not work. I have no problems passing one string whether (Len = *) or (LEN = 4)

I use VB to call Fortran a lot, passing values each way.  I have found that the simplest manner to pass string arrays in either direction is to write them to a text file and read them from the text file.  Everything else (real, real arrays, integer, integer arrays, single string) is not hard to do.

Mike

Mike,

I have a legacy fortran code that does exactly same thing, it reads and writes all the output to text files and then VB reads and writes thru text files. The intent to use DLL was to avoid using text files as it slows down the calculation time also it is very difficut to manage portability of code when we have a global user base.

 

Thanks,

Amit

Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya