DLL名为‘Find_Gamma_Peak.dll’,函数原型如下:extern void Find_Gamma_Peak(const double a[1024], double G_Peak_Channel[1024],double G_Peak_FWHM[1024], double G_Peak_Left[1024], double Peak_Right[1024],double G_Net_1[1024], double G_Bk_1[1024], double G_Net_2[1024], doubleG_Bk_2[1024], double Fit_area2[1024]);
转换为Delphi形式如下:procedure Find_Gamma_Peak(a_data:array of Double;va G_Peak_Channel:array of PDouble;G_Peak_FWHM:array of PDouble; G_Peak_Left:array of PDouble;var G_Peak_Right:array of PDouble;G_Net_1:array of PDouble;G_Bk_1:array of PDouble;G_Net_2:array of PDouble;G_Bk_2:array of PDouble;Fit_area2:array of PDouble);stdcall;External'Find_Gamma_Peak.dll';
var
G_Peak_Channel:array [0..1023] of PDouble;G_Peak_FWHM:array [0..1023] of PDouble; G_Peak_Left:array [0..1023] of PDouble; G_Peak_Right:array [0..1023] of PDouble; G_Net_1:array [0..1023] of PDouble; G_Bk_1:array [0..1023] of PDouble; G_Net_2:array [0..1023] of PDouble; G_Bk_2:array [0..1023] of PDouble; Fit_area2:array [0..1023] of PDouble;
procedure TForm1.Button3Click(Sender: TObject);
var
i,j,pinduan1:integer;
G_Peak_Channel1:array [0..1023] of Double;
G_Peak_FWHM1:array [0..1023] of Double;
G_Peak_Left1:array [0..1023] of Double;
G_Peak_Right1:array [0..1023] of Double;
G_Net_11:array [0..1023] of Double;
G_Bk_11:array [0..1023] of Double;
G_Net_21:array [0..1023] of Double;
G_Bk_21:array [0..1023] of Double;
Fit_area21:array [0..1023] of Double;
q,q1,q2:Double;
begin
for i:=0 to 1023 do
begin
a_data[i]:=1; //读取谱数据
end;
////////////////////////////////////////////////////////////////////////////
for i:=0 to 1023 do
begin
G_Peak_Channel[i]:=@G_Peak_Channel1[i]; //将数组的地址引入 指针变量
G_Peak_FWHM[i]:=@G_Peak_FWHM1[i];
G_Peak_Left[i]:=@G_Peak_Left1[i];
G_Peak_Right[i]:=@G_Peak_Right1[i];
G_Net_1[i]:=@G_Net_11[i];
G_Bk_1[i]:=@G_Bk_11[i];
G_Net_2[i]:=@G_Net_21[i];
G_Bk_2[i]:=@G_Bk_21[i];
Fit_area2[i]:=@Fit_area21[i];
GetMem(G_Peak_Channel[i],sizeof(PDouble)); //分配内存
GetMem(G_Peak_FWHM[i],sizeof(PDouble));
GetMem(G_Peak_Left[i],sizeof(PDouble));
GetMem(G_Peak_Right[i],sizeof(PDouble));
GetMem(G_Net_1[i],sizeof(PDouble));
GetMem(G_Bk_1[i],sizeof(PDouble));
GetMem(G_Net_2[i],sizeof(PDouble));
GetMem(G_Bk_2[i],sizeof(PDouble));
GetMem(Fit_area2[i],sizeof(PDouble));
end;
Find_Gamma_Peak(a_data,G_Peak_Channel,G_Peak_FWHM,G_Peak_Left,G_Peak_Right,G_Net_1,G_Bk_1,G_Net_2,G_Bk_2,Fit_area2); //调用dll函数
for i:=0 to 1023 do
begin
G_Peak_Channel1[i]:=G_Peak_Channel[i]^; //取返回结果。
G_Peak_FWHM1[i]:=G_Peak_FWHM[i]^;
G_Peak_Left1[i]:=G_Peak_Left[i]^;
G_Peak_Right1[i]:=G_Peak_Right[i]^;
G_Net_11[i]:=G_Net_1[i]^;
G_Bk_11[i]:=G_Bk_1[i]^;
G_Net_21[i]:=G_Net_2[i]^;
G_Bk_21[i]:=G_Bk_2[i]^;
Fit_area21[i]:=Fit_area2[i]^;
Memo1.Lines.Add(floattostrf(G_Peak_Channel1[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Peak_FWHM1[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Peak_Left1[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Peak_Right1[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Net_11[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Bk_11[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Net_21[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(G_Bk_21[i],ffFixed,8,2));
Memo1.Lines.Add(floattostrf(Fit_area21[i],ffFixed,8,2));
FreeMem(G_Peak_Channel[i]); //释放内存
FreeMem(G_Peak_FWHM[i]);
FreeMem(G_Peak_Left[i]);
FreeMem(G_Peak_Right[i]);
FreeMem(G_Net_1[i]);
FreeMem(G_Bk_1[i]);
FreeMem(G_Net_2[i]);
FreeMem(G_Bk_2[i]);
FreeMem(Fit_area2[i]);
end;
end;
在Delphi中编译通过,运行后出现内存异常错误。烦请哪位大神,帮看看问题出现在那块,解决思路是什么?
转换为Delphi形式声明如下:
procedure Find_Gamma_Peak(a_data:array of Double; G_Peak_Channel:array of Double; G_Peak_FWHM:array of Double; G_Peak_Left:array of Double; G_Peak_Right:array of Double; G_Net_1:array of Double; G_Bk_1:array of Double; G_Net_2:array of Double; G_Bk_2:array of Double; Fit_area2:array of Double);cdecl;External'Find_Gamma_Peak.dll'; //stdcall,Pascal,cdecl
var
G_Peak_Channel:array [0..1023] of Double;
G_Peak_FWHM:array [0..1023] of Double;
G_Peak_Left:array [0..1023] of Double;
G_Peak_Right:array [0..1023] of Double;
G_Net_1:array [0..1023] of Double;
G_Bk_1:array [0..1023] of Double;
G_Net_2:array [0..1023] of Double;
G_Bk_2:array [0..1023] of Double;
Fit_area2:array [0..1023] of Double;
procedure TForm1.Button3Click(Sender: TObject);
var
i,j,pinduan1:integer;
begin
for i:=0 to 1023 do
begin
a_data[i]:=1; //读取谱数据
end;
Find_Gamma_Peak(a_data,G_Peak_Channel,G_Peak_FWHM,G_Peak_Left,G_Peak_Right,G_Net_1,G_Bk_1,G_Net_2,G_Bk_2,Fit_area2); //调用dll函数
end;
在Delphi中编译通过,运行后出现内存异常错误,与前面方法等出现的错误一样。
SizeOf(PDouble) <> SizeOf(Double)
你好,主要测试很多方式最终都会出现内存异常,定义为Double数组,无返回值并且内存异常错误。定义PDouble,有返回值但内存异常错误。修改:GetMem(G_Peak_Channel[i],sizeof(G_Peak_Channel[i]));还是内存异常错误。
除此处会引起错误,还有那块能引起呢。
@迷雾206:
确定DLL中的导出函是 __stdcall 调用?
procedure Find_Gamma_Peak(a_data:array of Double...
procedure Find_Gamma_Peak(a_data: Pointer....
调用时AFun(@G_Peak_Channel, ...)
@jjw: 你好,DLL中只有a_data为输入参数,其它都G_..参数为返回值。调用我试了stdcall,Pascal,cdecl。都是指向一处地址内存出错。
DLL只有这些内容
// File: Find_Gamma_Peak.h
//
// MATLAB Coder version : 2.8
// C/C++ source code generated on : 01-Sep-2016 11:00:17
//
#ifndef __FIND_GAMMA_PEAK_H__
#define __FIND_GAMMA_PEAK_H__
// Include Files
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "rt_nonfinite.h"
#include "rtwtypes.h"
#include "Find_Gamma_Peak_types.h"
// Function Declarations
extern void Find_Gamma_Peak(const double a[1024], double G_Peak_Channel[1024],
double G_Peak_FWHM[1024], double G_Peak_Left[1024], double G_Peak_Right[1024],
double G_Net_1[1024], double G_Bk_1[1024], double G_Net_2[1024], double
G_Bk_2[1024], double Fit_area2[1024]);
#endif
//
// File trailer for Find_Gamma_Peak.h
//
// [EOF]
//
@迷雾206:
/*------ C++ -------*/
extern "C" __declspec(dllexport) void __stdcall copy(const double a[10], /*out*/ double b[10]) { for (int i = 0; i < 10; i++) { b[i] = a[i] + 0.1; } }
{ delphi }
procedure copy(const a: Pointer; b: Pointer); stdcall; external 'Project1.dll'; procedure TForm1.FormCreate(Sender: TObject); var I: Integer; a: array[0..9] of Double; b: array[0..9] of Double; begin for I := 0 to 9 do a[i] := 0.9; copy(@a, @b); for I := 0 to 9 do Memo1.Lines.Add(FloatToStr(b[i])); end;
Memo1 内容:
1 Memo1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1
@jjw: 非常感谢
procedure Find_Gamma_Peak(const a_data:Pointer;G_Peak_Channel:Pointer;G_Peak_FWHM:Pointer;G_Peak_Left:Pointer;G_Peak_Right:Pointer;G_Net_1:Pointer;G_Bk_1:Pointer;G_Net_2:Pointer;G_Bk_2:Pointer;Fit_area2:Pointer);stdcall;External'Find_Gamma_Peak.dll'; //stdcall,Pascal,cdecl
已经成功,一直查找问题方向错误。