Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

Passing an array of strings from VBA to Fortran DLL

cmc
New Contributor I
2,009 Views

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
Eugene_E_Intel
Employee
2,009 Views

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.

0 Kudos
DavidWhite
Valued Contributor II
2,009 Views

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
Eugene_E_Intel
Employee
2,009 Views

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

0 Kudos
cmc
New Contributor I
2,008 Views

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
Eugene_E_Intel
Employee
2,008 Views

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

0 Kudos
Reply