Delphi编写DLL动态库A,动态库中A调用动态库B
飞先生 2018-04-09 12:29:58 由于项目对接需要delphi编写A.DLL,在A.DLL中调用另外一个B.DLL进行读写卡。单独测试调用B.DLL没有问题,但封装A.DLL里面,再通过外部程序调用A.DLL时就报内存错误。
A.DLL内函数如下:
function ReadCard(iPortID:Integer;iBeep:Integer;var dm:Integer;var typeid:Integer;var userid:PChar;var watertabinfo:PChar):Integer;stdcall;
type
Tic_init=function(icdev:Integer;time:Integer):integer;stdcall; //初始化串口
Tdv_beep=function(icdev:Integer;time:Integer):integer;stdcall; //蜂鸣器
Tqt_T5557_read=function(icdev:Integer;Addr:Integer;Var Revbuffer:array of byte):integer;stdcall; //读取卡片
Tic_exit=function(icdev:Integer):integer;stdcall; //关闭串口
var
Rstr:string;
Tdll: Thandle; //加载DLL
Topencom:Tic_init;
Tbeep:Tdv_beep;
Tread:Tqt_T5557_read;
Tclose:Tic_exit;
FPointer:TFarProc;
reBuff:array[0..6,0..3] of byte;
fhz,fh,refh,i:Integer;
ss:array[1..28] of string;// 卡片的28字节
begin
Tdll := LoadLibrary('qtid.dll'); // 装载DLL文件
if Tdll > 0 then
begin
try
FPointer:=GetProcAddress(Tdll,PChar('ic_init')); // 查找初始化串口函数的位置
if FPointer<>nil then
begin
Topencom := Tic_init(FPointer);
fhz:= Topencom(iPortID-1,9600); // 调用初始化串口函数
if fhz > 0 then
begin
FPointer:=GetProcAddress(Tdll,PChar('qt_T5557_read'));// 查找读卡函数的位置
if FPointer<>nil then
begin
//SetLength(reBuff,4);
Tread:= Tqt_T5557_read(FPointer);
Rstr:='';
for i:=1 to 7 do
begin
fh:= Tread(fhz,i,reBuff[i-1]);
if fh=0 then Rstr:=Rstr + PChar(BytesToHex(reBuff[i-1]));
//SetLength(reBuff, 0);
end;
if length(Rstr)>50 then
begin
for i:=1 to 28 do ss[i]:=copy(Rstr,i*2-1,2);
//Rstr:='';
dm:=HexToDec(ss[1]); //厂家代码
if trim(ss[2])='11' then
begin
typeid := 1;//卡片类型为用户卡
userid:= PChar(IntToStr(HexToDec( ss[4]+ss[3] ))); //用户编号
end
else
begin
if Trim(ss[2])='22' then
begin
userid:='0';
case HexToDec(Trim(ss[5])) of
4: typeid := 9;//累加卡
5: typeid := 6; //参数设置卡
6: typeid := 11; // 测试卡
8: typeid := 7;//清零卡
9: typeid := 8; //
25: typeid := 13; //维修卡
26: typeid := 12; //检测卡
else
typeid := 9999;
end;
end
else
begin
typeid:=88;
userid:='77';
end;
end;
watertabinfo:=PChar(Rstr);
end
else
begin
watertabinfo:=PChar(Rstr);
dm:=44;
typeid:=66;
userid:='55';
end;
refh:=1;
if iBeep = 1 then
begin
FPointer:=GetProcAddress(Tdll,PChar('dv_beep')); //查找蜂鸣器函数的位置
if FPointer<>nil then
begin
Tbeep:= Tdv_beep(FPointer);
fh:= Tbeep(fhz,10);
end;
end;
end
else refh:=3;
end
else refh:=2;
FPointer:=GetProcAddress(Tdll,PChar('ic_exit'));// 查找关闭串口的位置
if FPointer<>nil then
begin
Tclose := Tic_exit(FPointer);
Tclose(fhz);
end;
end
else refh:=3;
Finally
FreeLibrary(Tdll); // 释放DLL,否则会一直占用内存,知道退出windows或关机为止;
End;
end
else refh:=4;
Result:= refh;
end;