Silverfrost Forums

Welcome to our forums

Running Fortran DLL in Excel VB

23 Sep 2010 9:06 #6992

Hi, I'm trying to get a dll file I created with Silverfrost FTN95 implemented into Excel via VB. I have the following fortran subroutine

SUBROUTINE SRK (C,F,NV,NL,NS,T,P,improved,BETA_IN,LIST,NEQ,Z,SOLID,BETA,Y)

which is defined in FORTRAN this way

PARAMETER (NCMAX=15,NFMAX=15)

INTEGER :: NEQ,F,C INTEGER :: improved INTEGER :: LIST(NCMAX),NV,NL,NS,SOLID(NCMAX)

DOUBLE PRECISION :: P,T DOUBLE PRECISION :: Y(NCMAX,NFMAX) DOUBLE PRECISION :: BETA_IN(NFMAX) DOUBLE PRECISION :: BETA(NFMAX) ,Z(NCMAX)

in VB I have defined it this way:

Declare Sub SRK Lib 'H:\Programs\LUWS (SRK) dll\LUWS_SRK.dll' (ByRef C As Integer, _ ByRef F As Integer, ByRef NV As Integer, ByRef NL As Integer, _ ByRef NS As Integer, ByRef T As Double, ByRef P As Double, _ ByRef improved As Integer, ByRef Betain As Double, ByRef LIST As Integer, _ ByRef NEQ As Integer, ByRef Z As Double, ByRef _ SOLID As Integer, ByRef BETA As Double, ByRef Y As Double)

My big problem is now that when I run my VB script, it never enters the fortran dll. It crashes for some reason and I think it is because Y is a 15x15 matrix. How do you define those in VB?

23 Sep 2010 10:31 #6994

There are two issues here.

  1. The VB arrays will probably be safe arrays with a header before the data that specifies the shape. Fortran arrays effectively have no header and the shape may be passed as an additional argument that is hidden in Fortran. I would have to check the details on this.

  2. I think that VB will use the STDCALL protocol and this is not the FTN95 default. There is some information on this in the FTN95 help file and (I think) in the Forum Knowledge Base.

23 Sep 2010 11:38 #6996

Many thanks for the reply. I've already added F_STDCALL subroutine in FORTRAN in order to make it visible for VB. I additionally tested the resulting dll with dumpbin.exe to see if all subroutines were exported. I compile my dll file this way:

FTN95 SRK.f95 /IMPORT_LIB SOURCE.dll slink SRK.OBJ SOURCE.dll -EXPORTALL -DLL /FILE:SRK.dll

This is what I have written in the header of the VB macro

Public Function srk(C As Integer, F As Integer, _ NV As Integer, NL As Integer, NS As Integer, T As Double, _ P As Double, improved As Integer, Bin As Range, LIST As Range, _ NEQ As Integer, Z As Range, SOLID As Range) As Variant

Dim Betain(1 To 15) As Double Dim Listin(1 To 15) As Integer Dim Zin(1 To 15) As Double Dim Solidin(1 To 15) As Integer Dim BETA(1 To 15) As Double Dim Y(1 To 15, 1 To 15) As Double

Please login to reply.