由于專案對接需要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;
uj5u.com熱心網友回復:
在這求助大神們
。自己先頂。
uj5u.com熱心網友回復:
function ReadCard(iPortID:Integer;iBeep:Integer;var dm:Integer;var typeid:Integer;var userid:PChar;var watertabinfo:PChar):Integer;cdecl; //把stdcall改成cdecl試試uj5u.com熱心網友回復:
估計是B.DLL的介面定義不正確,是用什么工具寫的,C的話把頭檔案發出來看看uj5u.com熱心網友回復:
你能不能除錯一下A.DLL的代碼,看到哪一行報錯了?這么看代碼太費勁了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/42795.html
上一篇:trunc 很奇怪的問題。
下一篇:TChromiumDevTools 控制元件在DELPHI中能看到,能拉出來,但是運行就提示不能識別,是什么原因啊
