Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel vba and Fortran dlls 1

Status
Not open for further replies.

BFG2

Technical User
Feb 4, 2003
23
0
0
GB
Was hoping someone could help.
Have been using Excel vba which is linked to Fortran functions within a dll for some time now.

However, I can't get it to work with subroutines which have more than 2 variables that are to be passed back.

Any suggestions? A simple example would be much appreciated

Cheers,

BFG
 
Hi,

Did you use the STDCALL naming convention on both sides ?
Anyway, remember that Fortran passes arguments by reference and STDCALL naming convention passes arguments by value, So you need to add a !DEC$ ATTRIBUTES REFRENCE :: declaration on Fortran side for arguments passed by ref (Visual Fortran syntax).

look at the example below

on Excel VBA side:

Code:
Declare Sub VBAtest Lib "V:\VBA test\Debug\VBA test.dll" (ByRef arg1 As Long, ByRef arg2 As Double, ByRef arg3 As Long, ByRef arg4 As Double)

.../...
Public Function VBtest() As Long
  Dim arg1 As Long
  Dim arg2 As Double
  Dim arg3 As Long
  Dim arg4 As Double
  '
  Call VBAtest(arg1, arg2, arg3, arg4)
  VBtest = 0
End Function

and on fortran side:

Code:
!  VBAtest.f90 
!
!  FUNCTIONS/SUBROUTINES exported from VBA test.dll:
!	VBAtest      - subroutine 
!
subroutine VBAtest(arg1, arg2, arg3, arg4)
!dec$ attributes stdcall, alias:'VBAtest', dllexport :: VBAtest
!dec$ attributes reference :: arg1, arg2, arg3, arg4

  ! Variables

  integer(4)	arg1, arg3
  real(8)	arg2, arg4

  ! Body of VBA test

  arg3 = irand(arg1)
  arg2 = acos(-1.D0) ! get value of Pi
  arg4 = arg2/2.D0

end subroutine VBAtest

Enjoy !
 
Hi
I have tried your example and I can't get it to work.
I get "Valueerror" in excel.
All stops whith the call to VBAtest. This is my code:

Declare Sub VBAtest Lib "K:\fortran vbatest.dll" (ByRef arg1 As Long, ByRef arg2 As Long)

Public Function vbtest() As Long
Dim arg1 As Long
Dim arg2 As Long
arg1 = 1 ' I suppose you want to pass a value
Call VBAtest(arg1, arg2)
vbtest = arg2 ' Pass a value to an excel-cell
End Function

Fortran
subroutine VBAtest(arg1,arg2)

integer*4 arg1,arg2
arg2=arg1*4

end

I linked as follow:
SLINK
* DLL
* lo vbatest.obj
* file vbatest.dll

it works ok to call from a fortranprogram

Can you please tell me what's wrong

Thank you

Giggi
 
I Giggi,

I think you forget to declare the stdcall calling convention on fortran side and to pass arguments by references. In Compaq visual fortran this declared as follows (as in my previous message).

Code:
!dec$ attributes stdcall :: VBAtest
!dec$ attributes refrence :: arg1, arg2

;-) - Phil31
 
Presious message continued

May be you also forget do export the entry point of your dll subroutine. A more complete declaration would be :

Code:
!dec$ attributes stdcall, alias: 'VBAtest', dllexport :: VBAtest
!dec$ attributes reference :: arg1, arg2

I don't know how to match theses rules with your version of Fortran (you don't tell anything about it) but I think you should find equivalent statements (use of pragmas, ...).

Good luck - Phil31 - ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top