Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28633 Discussions

Passing Array of Characters To VBA using a DLL

bjdesa
New Contributor I
243 Views

Passing Array of Characters

I have been trying to pass a character array from FORTRAN DLL to VBA Excel but I havent had any luck

Is there an easier way to do it than the way I have below?

subroutine testme1(A1,A2,A3,A4)

!dec$ attributes dllexport, stdcall, reference, alias : 'testme1' :: testme1

use testme10

implicit none

character*255 :: A1,A2,A3,A4

!character*255, dimension(4) :: char256

!char255(1) = A1

!char255(2) = A2

!char255(3) = A3

!char255(4) = A4

call testme

!A1 = char255(1)

!A2 = char255(2)

!A3 = char255(3)

!A4 = char255(4)

endsubroutine testme1

module testme10

implicit none

character*255, dimension(4) :: char255

data char255(:) / 'absd','dfsdf','sdsdfds','ssddtg'/

CONTAINS

subroutine testme

save

char255(1) ='abscde'

char255(2) ='fghijklm'

char255(3) ='nopqr'

char255(4) ='stuvwxyz'

endsubroutine testme

endmodule testme10

Public Declare Sub testme1 Lib "Z:\\Visual Studio 2008\\Projects\\Dll8\\Dll8\\Release\\Dll8.dll" (ByVal A1 As String, ByVal A2 As String, ByVal A3 As String, ByVal A4 As String, A20 As Long, A21 As Long, A22 As Long, A23 As Long)

Sub testme()

Dim char255(4) As String * 255

'integer4(1) = 255&;

'integer4(2) = 255&

'integer4(3) = 255&

'integer4(4) = 255&

char255(1) = "abcde"

char255(2) = "fghijkl"

char255(3) = "monoidj"

char255(4) = "dflivndsfv"

Call testme1(char255(1), char255(2), char255(3), char255(4), 255&, 255&, 255&, 255&)

'Call testme1(char255(1))

End Sub

0 Kudos
1 Reply
bjdesa
New Contributor I
243 Views
Thanks I found a method to do it.

FORTRAN:

subroutine fsubstringarray(c)
!dec$ attributes dllexport, stdcall, reference, alias : 'fsubstringarray' :: fsubstringarray
character*255 c(4)
c(1) = c(1)(1:index(c(1),' ') - 1)//'element1'
c(2) = c(2)(1:index(c(2),' ') - 1)//'element2'
c(3) = c(3)(1:index(c(3),' ') - 1)//'element3'
c(4) = c(4)(1:index(c(4),' ') - 1)//'element4'
end

VBA:

Option Explicit

Type stringarray
s(1 To 4) As String * 255
End Type
Dim sa As stringarray

Public Declare Sub fsubstringarray Lib "z:\visual studio 2008\projects\dll8\dll8\release\dll8.dll" (ByRef sa As stringarray, ByVal clen As Long)

Sub teststringarray()
sa.s(1) = "dskjbksad"
sa.s(2) = "fvdfhfthg"
sa.s(3) = "fgn"
sa.s(4) = "jkljgklg"
Call fsubstringarray(sa, 255&)
End Sub

0 Kudos
Reply