adem wrote:Now my problem is that I updated the MS Office to 64 bits and so far I have not been able to produce the .lib file. Any idea?
Building DLLs becomes drastically simpler if everything is 64-bit on Windows. But first, let's clear up some confusion about the need for some files:
The .lib and .def files are specifically needed for interaction with Microsoft's compilers. The .lib file is an import library, used (but not "required") by Microsoft's linker to understand the functions available in the the DLL that was built with Simply Fortran. The .def file is used to define "aliases" for Fortran procedures from the DLL in C code. The aliases, though, are a convenience because Microsoft assumes DLLs use a specific calling convention (stdcall) for 32-bit DLLs that are accompanied by particularly ugly procedure names.
The blog posts go over some of the nastier details of 32-bit stdcall name mangling (the addition of the @ symbol to function names followed by the number of bytes passed into the procedure), but 64-bit Windows doesn't suffer from this insanity.
So for your use case, calling 64-bit Fortran from 64-bit Excel, we can simplify things:
First, because you're not interacting with any of Microsoft's compilers in this case, we don't need to create .lib or .def files at all. Those steps were only needed to interact with Microsoft's compilers and to build 32-bit aliases for our function names that didn't look nutty. You can skip all those steps. Building your DLL in Simply Fortran is all you should need to do.
Second, 64-bit Windows DLLs conform to a single calling convention that doesn't rely on ugly name-mangling DLLs, and the calling convention is the same whether you're calling a Fortran function, a C function, a Pascal function, etc. We can skip right ahead to writing the VBA definitions. Once you build your DLL containing, for example:
function mean(x, n)
use iso_c_binding, only: C_PTR, C_DOUBLE, C_INT, c_f_pointer
implicit none
type(C_PTR), value::x
integer(kind=C_INT), value::n
real(kind=C_DOUBLE)::mean
real(kind=C_DOUBLE), dimension(:), pointer::fx
! Convert the C pointer of length n to an appropriate
! Fortran pointer
call c_f_pointer(x, fx, [n])
mean = sum(fx)/real(n, C_DOUBLE)
end function mean
You can immediately write your corresponding VBA definition:
Private Declare PtrSafe Function mean Lib "C:\Fortran\ExcelDll\fortran-library.dll" _
(ByRef x As Double, ByVal n As Integer) As Double
And it should work as expected.
There shouldn't really be anything else involved.
Jeff Armstrong
Approximatrix, LLC