DLL: extern "C" _declspec(dllexport) CString *Calc(int D_ITS,char sort, CString a[4])DELPHI: TArr=array[0..4] of string; PTArr=^Tarr; TCalcProc=function(D_its:integer;p:char;arr:TArr):PTArr;stdcall;总感觉函数声明有问题,但编译通过,调试时确实返回了值。但就是调用DLL函数后再释放就出错了。 delphi代码如下: procedure TForm1.Button1Click(Sender: TObject); type TArr=array[0..4] of string; PTArr=^Tarr; TCalcProc=function(D_its:integer;p:char;arr:TArr):PTArr;stdcall; Var c:TArr; p:PTArr; Calc_ITS:TCalcProc; D_its:integer; sort:char; Hand:THandle; begin Hand:=LoadLibrary('HNT.dll'); if Hand>0 then begin @Calc_ITS:=GetProcAddress(Hand,PChar('Calc')); if @Calc_ITS<>nil then begin c[0]:=Edit1.Text; c[1]:=Edit2.Text; c[2]:=Edit3.Text; c[3]:=Edit3.Text; c[4]:=Edit3.Text; if (Combobox1.text='C10') then D_its:=10; if (Combobox1.text='C15') then D_its:=15; if (Combobox1.text='C20') then D_its:=20; if (Combobox1.text='C25') then D_its:=25; if (Combobox1.text='C30') then D_its:=30; if (Combobox1.text='C40') then D_its:=40; if (Combobox1.text='C50') then D_its:=50; if (ComboBox2.Text='150X150X150') then sort:='A'; if (ComboBox2.Text='200X200X200') then sort:='B'; if (ComboBox2.Text='100X100X100') then sort:='C'; p:=Calc_ITS(D_its,sort,c); Edit4.text:=p^[0]; Edit5.text:=p^[1]; Edit6.text:=p^[2]; Edit7.text:=p^[3]; Edit8.text:=p^[4]; end else ShowMessage('DLL函数没找到') end else ShowMessage('DLL调用失败'); FreeLibrary(Hand);end; DLL段: extern "C" _declspec(dllexport) CString *Calc(int D_ITS,char sort, CString a[4]) { double a_temp[4];
应该是有资源未释放,比如在DLL打开的窗口句柄,线程之类的..检查DLL的实现代码,实在不行,不动态释放吧.
如果是调用DLL函数返回时出异常,那有可能函数声明约定不对.
extern "C" _declspec(dllexport) CString *Calc(int D_ITS,char sort, CString a[4])DELPHI:
TArr=array[0..4] of string;
PTArr=^Tarr;
TCalcProc=function(D_its:integer;p:char;arr:TArr):PTArr;stdcall;总感觉函数声明有问题,但编译通过,调试时确实返回了值。但就是调用DLL函数后再释放就出错了。
delphi代码如下:
procedure TForm1.Button1Click(Sender: TObject);
type
TArr=array[0..4] of string;
PTArr=^Tarr;
TCalcProc=function(D_its:integer;p:char;arr:TArr):PTArr;stdcall;
Var
c:TArr;
p:PTArr;
Calc_ITS:TCalcProc;
D_its:integer;
sort:char;
Hand:THandle;
begin
Hand:=LoadLibrary('HNT.dll');
if Hand>0 then begin
@Calc_ITS:=GetProcAddress(Hand,PChar('Calc'));
if @Calc_ITS<>nil then
begin
c[0]:=Edit1.Text;
c[1]:=Edit2.Text;
c[2]:=Edit3.Text;
c[3]:=Edit3.Text;
c[4]:=Edit3.Text; if (Combobox1.text='C10') then D_its:=10;
if (Combobox1.text='C15') then D_its:=15;
if (Combobox1.text='C20') then D_its:=20;
if (Combobox1.text='C25') then D_its:=25;
if (Combobox1.text='C30') then D_its:=30;
if (Combobox1.text='C40') then D_its:=40;
if (Combobox1.text='C50') then D_its:=50; if (ComboBox2.Text='150X150X150') then sort:='A';
if (ComboBox2.Text='200X200X200') then sort:='B';
if (ComboBox2.Text='100X100X100') then sort:='C';
p:=Calc_ITS(D_its,sort,c); Edit4.text:=p^[0];
Edit5.text:=p^[1];
Edit6.text:=p^[2];
Edit7.text:=p^[3];
Edit8.text:=p^[4]; end
else
ShowMessage('DLL函数没找到')
end
else
ShowMessage('DLL调用失败'); FreeLibrary(Hand);end;
DLL段:
extern "C" _declspec(dllexport) CString *Calc(int D_ITS,char sort, CString a[4])
{
double a_temp[4];
SelConst(sort);
a_temp[0]=(double)atof(a[0])*xs*1000/area;
a_temp[1]=(double)atof(a[1])*xs*1000/area;
a_temp[2]=(double)atof(a[2])*xs*1000/area;
a_temp[3]=(double)((a_temp[0]+a_temp[1]+a_temp[2])/3);
a[0].Format("%.1f",Xyue(a_temp[0],1));
a[1].Format("%.1f",Xyue(a_temp[1],1));
a[2].Format("%.1f",Xyue(a_temp[2],1));
a[3].Format("%.1f",Xyue(a_temp[3],1));
if (a_temp[3]>=D_ITS) a[4]="合格";
else a[4]="不合格";
return a;
}