In the meantime I found out what is causing the problem. There seems to
be a bug in the connection from gfortran to UCRT (Microsofts universal C
runtime, that ships with Windows 10 and 11). If I switch to a compiler
that uses MSVCRT (Microsoft Visual C runtime, an older library, but
still available) the problem disappears.

The port of the mingw64 I use comes in both flavors. So using the MSVCRT
version solves it.

Juergen

Am 18.02.25 um 13:56 schrieb Jürgen Bausa:
I am trying to compile some C and fortran code inserted into a dll to be
called by Excel. However, on each successful Fortran open statment Excel
crashes without any message. Everything else works perfectly so I am
sure the Excel/DLL interface works in principle.

I also found out, that the crash only happens if the open (for reading,
STATUS='OLD') statment is successful. So if I delete the file to open,
the program runs fine.

I then tried to call the same routine in the dll from a C program. That
works fine in both cases (file to open exists or does not exist).

As the problem arose in a very big project I tried to write a very
simple version that results in the same problem (see below).

At the moment I think the problem is most likely caused by libgfortrans
implementation of open. It seems to be somehow incompatible with Excel.
Is there a way to debug this? I was thinking about compiling my own
ligfortran and add some debug code to find out, where exactly the
problem is. Any other suggestions?

I am using Windows 11 and Excel 365 (both 64 Bit) and GCC 14.2.0 WIN64
from winlibs (https://winlibs.com/).

Here is my example:

f77open.f ---------------------------------------
       SUBROUTINE F77OPEN(STATUS)

       INTEGER   STATUS

       STATUS=0
       OPEN (UNIT=10, FILE='C:\Users\ztbau\btx.dat',
      2      ERR=100, IOSTAT=IOERR, STATUS='OLD')
       CLOSE(10)
       RETURN

  100  STATUS=2
       RETURN

       END
-------------------------------------------------
f77open.h ---------------------------------------
#include <stddef.h>
#ifdef __cplusplus
#include <complex>
#define __GFORTRAN_FLOAT_COMPLEX std::complex<float>
#define __GFORTRAN_DOUBLE_COMPLEX std::complex<double>
#define __GFORTRAN_LONG_DOUBLE_COMPLEX std::complex<long double>
extern "C" {
#else
#define __GFORTRAN_FLOAT_COMPLEX float _Complex
#define __GFORTRAN_DOUBLE_COMPLEX double _Complex
#define __GFORTRAN_LONG_DOUBLE_COMPLEX long double _Complex
#endif

/* Prototypes for external procedures generated from f77open.f
    by GNU Fortran (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht
Sanders, r3) 14.2.0.

    Use of this interface is discouraged, consider using the
    BIND(C) feature of standard Fortran instead.  */

void f77open_ (int *status);

#ifdef __cplusplus
}
#endif
---------------------------------------------------
dlltest2.c ----------------------------------------
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "f77open.h"


int WINAPI dlltest2()

{
     int status=0;

     f77open_ (&status);

     return status;
}
----------------------------------------------------

to compile I use the following commands:
----------------------------------------------------
gcc -march=native -fno-diagnostics-color -Wall -c -o dlltest2.o dlltest2.c
gfortran -march=native -fno-diagnostics-color -Wall -c f77open.f
gcc -march=native -fno-diagnostics-color -shared -Wl,--add-stdcall-alias
dlltest2.o f77open.o -o dlltest2.dll -lgfortran
----------------------------------------------------

Regards,

Juergen

Reply via email to