VC中的dll:
// tryt1.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
 )
{
    return TRUE;
}typedef struct input
{
int iN;
int iM;
double iC_RON[NN];
double iC_TD[NN];
double iC_MT[NN];
double iP_RON[MM];
double iP_MT[MM];
}INPUT;
typedef struct output
{
double chromosome[NN*MM];
double cp[MM];
double fitness[MM];
double value;
}OUTPUT;extern "C"_declspec(dllexport) OUTPUT WINAPI tryt1( INPUT in, OUTPUT *out )
delphi中调用dll:type
  input = Record
    N:integer;
    M:integer;
 iC_RON:array [1..NN] of double;
 iC_TD:array [1..NN] of double;
 iC_MT:array [1..NN] of double;
 iP_RON:array [1..MM] of double;
 iP_MT:array [1..MM] of double; end;  output = Record
  chromosome:array[1..NN*MM]of Double;
  cp:array[1..MM]of Double;
  fitness:array[1..MM]of Double;
  value:Double;  end;  TPoutput = ^output;

function tryt1(inx : input; outx:TPoutput): output;  stdcall;   external 'tryt1.dll' index 1;procedure TForm1.Button1Click(Sender: TObject);
var
   inx : input;
   outx : output;
   iResult:output;
  iResult := tryt1( inx, @outx );  showmessage( floattostr(iResult.value));
为什么编译通过后,出现“Project1.exe raised exception class EInvalidOp with message ''Invalid floating point operation' Process stopped. ”
这是什么原因?

解决方案 »

  1.   

    改成下面这两句代码试试//function tryt1(inx : input; outx:TPoutput): output;  stdcall;   external 'tryt1.dll' index 1;
    function tryt1(inx : input; var outx:output): output;  stdcall;   external 'tryt1.dll' index 1;
    //iResult := tryt1( inx, @outx );
    iResult := tryt1( inx, outx );
      

  2.   

    程序结构及调用上没问题(我试过,用vc测试)
    TRYT1_API OUTPUT tryt1( INPUT in, OUTPUT *out )
    {
    OUTPUT p;
    p.value=200;
    return p;
    }
    delphi的结果为200Invalid floating point operation
    一般为浮点计算出错,查你的dll的tryt1函数
      

  3.   

    是哪一句出错?是 showmessage( floattostr(iResult.value)) 吗?在调试器中看一下iResult.value的值是多少?--
    http://www.agui.googlepages.com
    mailto: agui.cn(a)gmail.com
      

  4.   

    type
      input = Record
        N:integer;
        M:integer;
     iC_RON:array [1..NN] of double;
     iC_TD:array [1..NN] of double;
     iC_MT:array [1..NN] of double;
     iP_RON:array [1..MM] of double;
     iP_MT:array [1..MM] of double; end;这结构的对齐方式需要和C里一致
    type
      input = packed record //<<<
        N:integer;
        M:integer;
        iC_RON:array [0..NN-1] of double;
        iC_TD:array [0..NN-1] of double;
        iC_MT:array [0..NN-1] of double;
        iP_RON:array [0..MM-1] of double;
        iP_MT:array [0..MM-1] of double;
     end;
    //同样
      output = packed record
        chromosome: array[0..NN*MM - 1]of Double;
        cp: array[0..MM-1]of Double;
        fitness: array[0..MM-1]of Double;
        value: Double;
      end;
      

  5.   

    多谢楼上各位的帮忙,谢谢了,但是我都试了一下,还是不行。怎么办呢?to agui:
    对的,就是这句showmessage( floattostr(iResult.value)) 出错的,我是刚学delphi的,怎么在调试器中看它的值?
      

  6.   

    double和int估计不用packed了,它们都占据了字长的整数倍。--
    http://www.agui.googlepages.com
    mailto: agui.cn(a)gmail.com