Erik Thysell Erik Thysell - 1 month ago 26
C++ Question

Error when calling c++ dll from delphi

I am trying to use create a c++ wrapper (using MSVS2013) for some fortran code. The wrapper is supposed to be called from DELPHI (old code created in DELPHI 5 - which I cannot touch). (DELPHI -> C++ dll -> fortran dll)
The aim is that some data

struct
s (
record
in delphi) with
int
's and
double
's and
double[]
's are passed from delphi to fortran for calculations.
The c++ dll -> fortran is no problem - that I have tested with another c++ executable. It works. The problem is when delphi calls c++ and c++ calls fortran. Then I get an error that says


Project "myDelphiProject.exe" raised exception class EZeroDivide with message 'Floating point division by zero'.


There is no problem calling the c++ wrapper if the call to fortran does not happen.
I have tested and confirmed that all variables get through between delphi and c++. Calling convention is set to
cdecl
.

Simplified code:
Delphi

interface
rec1 = record
var1,var2: double;
end;
procedure myCppWrap(record1: myrecord); cdecl; external 'mycppwrap.dll' name '_myCppWrap'
implementation
procedure MyDelphiFunc();
var
...
begin
myCppWrap(rec1)
...


Simplified c++ code:

struct rec1
{
double var1,var2;
}
extern "C" __declspec(dllexport) __cdecl myCppWrap(rec1 r1);
extern "C" *myFortranSub(double *varF1,double *varF2);


In short:
DELPHI -> C++ works, C++ -> Fortran works, DELPHI -> C++ -> Fortran does not work.

Does anyone have any idea of what is wrong?




EDIT:
OK.. so I have managed to narrow it down:
I have division by zero that results in infinity (which is a correct internal result with the given input - this is not direct output from fortran dll).
This goes well when being called by c++ but not from delphi when the fortran dll is compiled using default release settings.
However it does work when using debug settings, both in c++ and delphi.
I output variables to a text file (before and after division) for verification and in all 4 cases the divisor is 0 and the result is 'inf' in the three cases that goes well. Both in debug and release mode I have the same setting for floating point exception handling "Produce NaN, signed infinities, and denormal results".

This turns out to be more of a compiler setting/flag problem than coding...

Anyone have any experience with this??
(I am working on a small and verifiable code piece)

Answer

So following @David Heffeman's advice... adding

use ifcore

and

fpe_old_flags = FOR_SET_FPE(0)

in the beginning of the code solved it. Thanks a lot David!