Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
43 Views

How to pass a char array from Fortran to EXCEL VBA?

Hi

I am making a fortran dll that I can call from EXCEL VBA and I also need to have the fortran dll to call another function defined in VBA. This function to be called from Fortran must have an argument that is a char array.Here is what I am trying to do:

VBA code to call a fortran subroutine defined in dll:

Declare PtrSafe Sub NPTEST Lib "D:\SNOPT\SNOPTDLL\SNOPTDLL\Release\SNOPTDLL.dll" Alias "_NPTEST@8" (id As Long, ByVal confun As Long)

Sub testtest()
    Dim id
    id = 2
    Call NPTEST(id, AddressOf funtest)
End Sub

Here is the fortran subroutine:

subroutine nptest(id, confun)


      !DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE :: NPTEST
      !DEC$ ATTRIBUTES ALIAS:"_NPTEST@8" :: NPTEST

      !DEC$ ATTRIBUTES REFERENCE :: id
      !DEC$ ATTRIBUTES REFERENCE :: confun

      implicit double precision (a-h,o-z)

      external confun

      character*8 cw(50)
      
      mode = id
      .....
      
      call confun(mode, cw)
      call confun(mode, cw)
      call confun(mode, cw)
      call confun(mode, cw)

      return
    end subroutine

and here is the VBA function called by the fortran dll:

Sub funtest(mode As Long, ptr As Variant)

    MsgBox mode
    mode = mode + 1
End Sub

 

As you can see I need to pass a pointer to the char array cw to the VBA function but I am not sure what data type I should use for ptr in the VBA function. I tried many types and EXCEL always crashes after the last function call (It is called 4 times from Fortran). if cw is double array I have no problem and everything works, but as soon as I change cw to char array it crashes. Any suggestions?

 

 

 

 

 

0 Kudos
0 Replies