Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1 View

Passing an array of strings from VBA to Fortran DLL

Hi,

I have difficulties getting the DLL to read my array of strings. The following declaration is given in VBA:

    Public Declare Sub fortran_dll Lib "path_to_dll" _
    (ByVal array1 As String, ByRef array2 As Integer, ByRef array3 As Double, _
    ByRef array4 As Integer, ByRef array5 As Double)

The following attributes are given in Fortran:

    !dec$ attributes dllexport, stdcall, reference, alias: "fortran_dll" :: fortran_dll
    !dec$ attributes reference :: input_characters, input_integers, input_reals
    !dec$ attributes reference :: output_integers, output_reals

Is there a guide or procedure for how to make this work? I manage to read the integers and doubles, so the trouble is only linked to the strings. Can anyone help me?

\Carl

0 Kudos
5 Replies
Highlighted
Employee
1 View

Please post some sample code

Please post some sample code that shows how you access the strings.

This thread has sample code for passing VBA strings to Fortran: https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/393803

Arrays of strings would be more difficult, since VBA probably uses "SafeArray of BSTR" structure for array of strings.

Eugene Epshteyn
0 Kudos
Highlighted
Black Belt
1 View

I avoided the use of the BSTR

I avoided the use of the BSTR structures in my application by concatentating all of the strings, and so only passing back a single string.  It needs to be a C string, e.g.

Str=CharOut(1)//CharOut(2)//CharOut(3)//CharOut(4)//CharOut(5)//''C

 

0 Kudos
Highlighted
Employee
1 View

How do you declare fortran

How do you declare fortran_dll() and its parameters in Fortran?

Eugene Epshteyn
0 Kudos
Highlighted
Beginner
1 View

Sample code:

Sample code:

!   declarations:   
    character(len=256), intent(in) ::  input_characters(5)

!   input_characters:
    character(len=50)              :: file_1, file_2, file_3, file_4, file_5
 
!   assigning characters:
    file_1                         = input_characters(1)
    file_2                         = input_characters(2)
    file_3                         = input_characters(3)
    file_4                         = input_characters(4)
    file_5                         = input_characters(5)

I have tried different character lengths in the declaration, with no luck. This post (https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/391172) address the same issue I have, but I only see workarounds (e.g. write the strings to a text-file and read the strings from there), not an actual solution to the problem. I see "SafeArray of BSTR" is mentioned in both posts. Can someone explain to me how I do this? Does that mean I have to translate the string to ASCII or binary code and send that instead?

0 Kudos
Highlighted
Employee
1 View

Quote:David White wrote:

David White wrote:

I avoided the use of the BSTR structures in my application by concatentating all of the strings, and so only passing back a single string.  It needs to be a C string, e.g.

Str=CharOut(1)//CharOut(2)//CharOut(3)//CharOut(4)//CharOut(5)//''C

If a single string is passed to a Fortran routine, then Fortran routine needs to read it as a single string, not as an array.

I suggest that in VBA you concatenate your array of strings into a one string, using end-of-line as a delimiter.  Pass this concatenated string to Fortran code, and then parse it into individual strings.

Handling SafeArray of BSTR from Fortran code would be very difficult.

Regards,

--Eugene

Eugene Epshteyn
0 Kudos