- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the hint.
It is a lot more complicated than I thought, maybe I should try a workaround.
Thank you again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page