delphi 写的DLL 结构指针传递有关问题
delphi 写的DLL 结构指针传递问题
dll结构
type
TCardInfo = packed record // the Communication Block used in both parts (Server+Client)
consequence: Integer; //标志
H_cardsn: array[0..9] of char; //卡序列号
Name: array[0..15] of char; //姓名
Sex: array[0..1] of char; //性别
end;
pCardInfo = ^TCardInfo;
函数
function ReadCard1(var tag_: Integer; RCardInfo:pCardInfo): integer; stdcall;
begin
getMem(_pCardInfo, sizeof(TCardInfo));
_pCardInfo^.consequence := 0;
StrPCopy(_pCardInfo^.H_cardsn, '0000FFFFFF');//卡序列号
StrPCopy(_pCardInfo^.Name, '张三'); //姓名
StrPCopy(_pCardInfo^.Sex, '男'); //性别
RCardinfo := _pCardInfo;
freemem(_pCardInfo);
end;
现在我要在主程序中调用DLL
function ReadCard1(tag_: Integer; RCardInfo: pCardInfo): integer; stdcall; external 'ReadCard.dll' name 'ReadCard1';
procedure TForm1.btn1Click(Sender: TObject);
var i: Integer;
str: string;
CardInfo: TCardInfo;
begin
try
ReadCard1(0, @CardInfo);
edt3.Text := string(CardInfo.H_cardsn);
except
on e: exception do
showmessage(e.Message);
end;
end;
现在主程序调用时提示‘read of address 00000000’
请问我上面的DLL程序和主程序是否都写的正确,不知为何会出错。求解。谢谢
我是想在主程序中取出DLL中返回的结构中的值,请问该如何做?
------解决方案--------------------
你的pCardInfo要由调用者分配和释放内存
------解决方案--------------------
按我写的改, 你先试试
function ReadCard1(var tag_: Integer; RCardInfo:pCardInfo): integer; stdcall;
begin
if Assigned(RCardInfo) then
begin
RCardInfo^.consequence := 0;
StrPCopy(RCardInfo^.H_cardsn, '0000FFFFFF');//卡序列号
StrPCopy(RCardInfo^.Name, '张三'); //姓名
StrPCopy(RCardInfo^.Sex, '男'); //性别
Result:= 0;
end
else begin
result:= -1;
end;
end;
你调用的那段代码就不要改了, 就那么写
------解决方案--------------------
建议在主程序分配,释放
dll结构
type
TCardInfo = packed record // the Communication Block used in both parts (Server+Client)
consequence: Integer; //标志
H_cardsn: array[0..9] of char; //卡序列号
Name: array[0..15] of char; //姓名
Sex: array[0..1] of char; //性别
end;
pCardInfo = ^TCardInfo;
函数
function ReadCard1(var tag_: Integer; RCardInfo:pCardInfo): integer; stdcall;
begin
getMem(_pCardInfo, sizeof(TCardInfo));
_pCardInfo^.consequence := 0;
StrPCopy(_pCardInfo^.H_cardsn, '0000FFFFFF');//卡序列号
StrPCopy(_pCardInfo^.Name, '张三'); //姓名
StrPCopy(_pCardInfo^.Sex, '男'); //性别
RCardinfo := _pCardInfo;
freemem(_pCardInfo);
end;
现在我要在主程序中调用DLL
function ReadCard1(tag_: Integer; RCardInfo: pCardInfo): integer; stdcall; external 'ReadCard.dll' name 'ReadCard1';
procedure TForm1.btn1Click(Sender: TObject);
var i: Integer;
str: string;
CardInfo: TCardInfo;
begin
try
ReadCard1(0, @CardInfo);
edt3.Text := string(CardInfo.H_cardsn);
except
on e: exception do
showmessage(e.Message);
end;
end;
现在主程序调用时提示‘read of address 00000000’
请问我上面的DLL程序和主程序是否都写的正确,不知为何会出错。求解。谢谢
我是想在主程序中取出DLL中返回的结构中的值,请问该如何做?
------解决方案--------------------
你的pCardInfo要由调用者分配和释放内存
------解决方案--------------------
按我写的改, 你先试试
function ReadCard1(var tag_: Integer; RCardInfo:pCardInfo): integer; stdcall;
begin
if Assigned(RCardInfo) then
begin
RCardInfo^.consequence := 0;
StrPCopy(RCardInfo^.H_cardsn, '0000FFFFFF');//卡序列号
StrPCopy(RCardInfo^.Name, '张三'); //姓名
StrPCopy(RCardInfo^.Sex, '男'); //性别
Result:= 0;
end
else begin
result:= -1;
end;
end;
你调用的那段代码就不要改了, 就那么写
------解决方案--------------------
建议在主程序分配,释放