I'm not sure if this is the right group for this topic, but you guys
here seem to be relatively friendly towards newbies, so I'll take my
chances.
I've been using fortran for some time, but only for "simple programs"
(exe file as a result of subroutines). I compiled it from command
line, and it has covered my needs. I'm using CVF 6.6.c under Windows
XP, with Office 2007 installed.
Now I'm trying to compile/build a dll file which I will be able to
call in excel as a function. After reading the manual (which still
confuses me), and some tutorials off the net
(http://newtonexcelbach.wordpress.com/2008/12/20/solving-simultaneous-equations-fortran-dll/)
and some youtube videos (youtube: excel fortran - 4 part video), I'm
still having trouble doing it in my case.
I'm trying to make a simple example work
!*******************************************
! FUNCTIONS/SUBROUTINES exported from zbroj.dll:
! zbroj - subroutine
PURE FUNCTION zbroj(X)
! Expose subroutine zbroj to users of this DLL
!DEC$ ATTRIBUTES DLLEXPORT::zbroj
REAL, INTENT(IN) :: X
zbroj = 2 * X *100
END FUNCTION zbroj
!*******************************************
(it is an example from CVF manual book)
which in excel I can call. I compiled and builded it as DLL and copied
it into \win\system32 directory, but still, excel doesn't recognize
it.
Please, I know this is "homework" and trivial work to some of you, but
if you could give me a few pointers as to what I'm doing wrong, just
to get me started with this one, I'd be really grateful.
Regards, Matjaz
This is a FAQ. You have not shown the declarations section from the
VBA (Visual Basic for Applications) module which must be inserted into
your workbook.
I compiled my DLL from the command line (using DVF 5)
df /dll zbroj.f90
CVF and DVF use the STDCALL convention by default. This changes the
name exported by the routine to
_ZBROJ@4
The @4 suffix corresponds to a single argument (4 bytes of pointer)
passed on the stack.
If you declare your function as follows:
Declare Function zbroj Lib "zbroj.dll" Alias "_ZBROJ@4" (x As Single)
As Single
It then works properly. Note that I have placed my copy of the dll in
the "Documents" folder with the workbook. YMMV.
Otherwise you can take care of the name mangling from the Fortran
side:
...
!DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'zbroj' ::zbroj
...
and on the VBA side
Declare Function zbroj Lib "zbroj.dll" (x As Single) As Single
In a cell place
=zbroj(1)
and see
200
Also note that you have to change your security settings to allow
macros OR enable macros manually after opening the workbook.
HTH
--- e
The mixed language programming documentation in CVF is excellent. I
would strongly recommend Ring TFM.
That's R-ing TFM. Sorry for any confusion.
Matjaz, likely a person for whom English is not his first language, has
quoted the manual already. He might not know that we mean "The Fortran
Manual" around here.
Maybe someone with cvf could give him a reference therein.
I'd sure like to have an implementation that had a manual worthy of it.
--
George
The men and women of Afghanistan are building a nation that is free, and
proud, and fighting terror - and America is honored to be their friend.
George W. Bush
Picture of the Day http://apod.nasa.gov/apod/