s-news
[Top] [All Lists]

.Fortran call of DLL causing Splus exit

To: s-news@wubios.wustl.edu
Subject: .Fortran call of DLL causing Splus exit
From: Kevin_Brand@hc-sc.gc.ca
Date: Thu, 15 Nov 2001 16:56:18 -0500
Cc: kbrand@uottawa.ca

Dear S-Plus users,

I am having trouble using a fortran-compiled DLL in Splus.
Briefly, a function call to the fortran-compiled DLL causes an exit from S-PLUS!
After reading several posts related to linking fortan to Splus via DLLs'
(posted on the S-user archives), I am still having problems.
I have spent several days trying to narrow in on the problem. I am nearing my
wits end.

Platform Details: I'm working on Windows 95, using Splus-2000 (Release 1) and
using Fortran Powerstation 4.0.

My most direct question is how can i get Splus to give me some clues about the
type
of error that is causing it to exit.  I describe the specifics of my problem in
more detail below.

I have the following code in my Fortran program which as I understand acts as a
substitute for a formal .DEF file.

                    !MS$ATTRIBUTES DLLEXPORT :: MYFUN

(it occurs as one of the first statements in my main fortran subroutine)

I up-load the DLL file in Splus using the following code

     if(!is.dll.loaded("myfun.dll")) {
          dll.load("C:\\MSDEV\\Projects\\myfun\\Debug\\myfun.dll", symbols =
list(myfun =
               "_MYFUN@56"))
     }

I have used Windows-explorer (Quick-view) to examine my DLL, confirming that it
does indeed declare a symbol for my fortran Subroutine.  That symbol's
definition,
     _MYFUN@56

is compatible with the command I've used in the call to dll.load.
(BTW, I'm told the 56 makes sense.  I understand from previous posts that it
represents
the number of arguments times 4 ... I have 14 arguments to myfun)

Finally, I have an splus calling function which calls the Fortran symbol.  The
relevant
code is as follows.

     storage.mode(x1) <- "integer"
     storage.mode(x2) <- "integer"
     storage.mode(x3) <- "single"
     :    :    :    :
     storage.mode(x14) <- "single"
     res <- .Fortran("myfun",
          x1,
          x2,
          x3 = X3,
          ::    ::,
          x14)

The argument order and type are consistent between the Splus and Fortran code.
Note also that exact same code worked on a unix platform
(albeit with dyn.load replacing the role of dll.load), which seems to confirm
to me that the arguments must be compatable!

When I run my Splus program, the dll loads successfully
however the first call the the fortran subroutine causes an exit from Splus
(leaving me
no explicit clues regarding the problem).

I think I can ruel out the following.
1. A simple mismatch in argument type or order.
     - A dummy version of my DLL consisting of only the subroutine definition
     including all arguments and a few rudimentary expressions works succesfully
     (i.e., as a. Fortran call in splus).
     - The exact same code worked on Unix platform using dyn.load()
2. An obvious problem with the fortran code
     - I have set-up a main program in fortran and called the very same Fortran
subroutine
     constituting the DLL.  It works.  This suggests to me that the problem lies
 in the
     interface between Splus and the compiler.


 I have tried all the sensible permutations for call.convention and MS
attributes in
the fortran code.  None of them work.

I would be greatly appreciative of any suggestions about what to
be on the look out for.  Are Unix compilers known to be more forgiving about
certain details than Windows compilers ?

Some added detail is shown below for those who are interested.

My suspicions
1. My fortran program calls several other subroutines (but they were compiled as
 part
of the DLL making process).  Could this be causing problems?

2. I pass several arrays from Splus to the fortran subroutine and then from
there
to several other subroutines.  My approach here has been to pass the dimensions
along as separate arguments in the subroutines, and dimension accordingly.
Perhaps
the Splus interface imposes tougher restrictions?

3. Perhaps I require an explicit DEF file rather than the DLLEXPORT option (does

anyone have an example DEF file for me to draw from).  This may allow me to be
explicit about which arguments require pointer-references rather than
value-references
(i.e., the arrays).





<Prev in Thread] Current Thread [Next in Thread>