调用其他语言dll出现access violation ataddress问题

默__ 2013-06-28 08:44:29
公司给出的接口说明
读二代身份证
原型: int ReadCard(char *Name,
char *Sex,
char *Nation,
char *Birthday,
char *Address,
char *ID,
char *Department,
char *StartDate,
char *EndDate,
char *Reserve,
char *AppAddress1,
char *PhotoData)
参数: Name 姓名
Sex 性别
Nation 民族
Birthday 生日
Address 住址
ID 身份证号
Department 发证机关
StartDate 起始日期
EndDate 终止日期
Reserve 预留信息
AppAddress1 预留地址
PhotoData 照片信息
返回:
=0 成功
<0 失败
程序如下
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
OpenCom = function (Comport:integer;Baud:integer):integer;stdcall;
SelModule = function (PortType:integer;Module:integer):integer;stdcall;
CloseCom = function ():integer;stdcall;
ReadCard = function (Name:pChar;Sex:pChar;Nation:pChar;Birthday:pChar;
Address:pChar;ID:pChar;Department:pChar;StartDate:pChar;EndDate:pChar;
Reserve:pChar;AppAddress1:pChar;PhotoData:pChar):integer;stdcall;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var DllHandle:THandle;
ComPort,Baud,HBaud,result:integer;
PortType,Module:integer;
sfzName,sfzSex,sfzNation,sfzBirthday,sfzAddress,sfzID,sfzDepartment,
sfzStartDate,sfzEndDate,sfzReserve,sfzAppAddress1,sfzPhotoData:string;
oc:OpenCom;
sm:SelModule;
cc:CloseCom;
rc:ReadCard;
begin

ComPort:=1;Baud:=9600;HBaud:=115200;result:=22;
PortType:=0;Module:=3;

DLLHandle:=LoadLibrary('JCJP_API.dll');
if DLLHandle<>0 then
begin
@oc:=GetProcAddress(DllHandle,'Open_Com');

if @oc<>nil then
begin
if( oc(ComPort,Baud) = 0 )then
begin
@sm:= GetProcAddress(DllHandle,'Sel_Module');
if @sm<>nil then
begin
if(sm(PortType,Module) = 0)then
begin
@cc:= GetProcAddress(DllHandle,'Close_Com');
if( cc()=0)then
begin
@oc:=GetProcAddress(DllHandle,'Open_Com');
if(@oc<>nil)then
begin
if(oc(ComPort,HBaud)=0)then
begin
@rc:= GetProcAddress(DllHandle,'ReadCard');
if @rc<>nil then
begin
try
if(rc(@sfzName,@sfzSex,@sfzNation,@sfzBirthday,@sfzAddress,@sfzID,@sfzDepartment,
@sfzStartDate,@sfzEndDate,@sfzReserve,@sfzAppAddress1,@sfzPhotoData)=0)then
begin
// showmessage(sfzID);
showmessage('222'+sfzName);
showmessage('222');
end
else
begin
showmessage('没有找到二代身份证,读取失败!');
end;
except
on E:exception do
showmessage(e.Message);
end;
result:=sm(PortType,0);
result:=cc();
end
else
showmessage('rc is nil');
end
else
showmessage('高频打开串口失败');
end
else
showmessage('oc is nil');
end
else
showmessage('关闭串口失败');
end
else
begin
showmessage('选择模式失败');
end;
end
else
begin
showmessage('sm is nil');
end;
end
else
showmessage('串口打开失败!');
end
else
showmessage('oc is nil');
end
else
showmessage('222');
FreeLibrary(DLLHandle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;


end.

-----------------------------------------
把下面这句删掉一切正常。
rc( sfzName,@sfzSex,@sfzNation,@sfzBirthday,@sfzAddress,
@sfzID,@sfzDepartment,@sfzStartDate,@sfzEndDate,@sfzReserve,
@sfzAppAddress1,@sfzPhotoData)=0
请高手帮忙look look
...全文
674 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
默__ 2013-06-28
  • 打赏
  • 举报
回复
结束按钮事件的时候弹出上面的框框。
默__ 2013-06-28
  • 打赏
  • 举报
回复
引用 10 楼 gxgyj 的回复:
多设断点,仔细看,是在哪报错 慢慢来吧
调试到最后,好像到也没有错。 是不是需要释放什么?
gxgyj 2013-06-28
  • 打赏
  • 举报
回复
多设断点,仔细看,是在哪报错 慢慢来吧
默__ 2013-06-28
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
OpenCom = function (Comport:integer;Baud:integer):integer;stdcall;
SelModule = function (PortType:integer;Module:integer):integer;stdcall;
CloseCom = function ():integer;stdcall;
ReadCard = function (Name,Sex,Nation,Birthday,Address,ID,Department,
StartDate,EndDate,Reserve,AppAddress1,PhotoData:PAnsiChar):integer;stdcall;

TIDCardInfo=record
_Name:array[1..31] of AnsiChar;
_Sex:array[1..3] of AnsiChar;
_Nation:array[1..51] of AnsiChar;
_Birthday:array[1..9] of AnsiChar;
_Address:array[1..51] of AnsiChar;
_ID:array[1..19] of AnsiChar;
_Department:array[1..51] of AnsiChar;
_StartDate:array[1..9] of AnsiChar;
_EndDate:array[1..9] of AnsiChar;
_Reserve:array[1..51] of AnsiChar;
_AppAddress1:array[1..51] of AnsiChar;
_PhotoData:array[1..51] of AnsiChar;
Freadinfo:Integer; //读卡状态
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var DllHandle:THandle;
ComPort,Baud,HBaud,result:integer;
PortType,Module:integer;
oc:OpenCom;
sm:SelModule;
cc:CloseCom;
rc:ReadCard;
idci:TIDCardInfo;
begin

ComPort:=1;Baud:=9600;HBaud:=115200;result:=22;
PortType:=0;Module:=3;

DLLHandle:=LoadLibrary('JCJP_API.dll');
if DLLHandle<>0 then
begin
@oc:=GetProcAddress(DllHandle,'Open_Com');

if @oc<>nil then
begin
if( oc(ComPort,Baud) = 0 )then
begin
@sm:= GetProcAddress(DllHandle,'Sel_Module');
if @sm<>nil then
begin
if(sm(PortType,Module) = 0)then
begin
@cc:= GetProcAddress(DllHandle,'Close_Com');
if( cc()=0)then
begin
@oc:=GetProcAddress(DllHandle,'Open_Com');
if(@oc<>nil)then
begin
if(oc(ComPort,HBaud)=0)then
begin
@rc:= GetProcAddress(DllHandle,'ReadCard');
if @rc<>nil then
begin
idci.Freadinfo:=rc(@idci._Name,@idci._Sex,@idci._Nation,
@idci._Birthday,@idci._Address,@idci._ID,@idci._Department,
@idci._StartDate,@idci._EndDate,@idci._Reserve,@idci._AppAddress1,
@idci._PhotoData);
showmessage('没有找到二代身份证,读取失败!');


result:=sm(PortType,0);
result:=cc();
end
else
showmessage('rc is nil');
end
else
showmessage('高频打开串口失败');
end
else
showmessage('oc is nil');
end
else
showmessage('关闭串口失败');
end
else
begin
showmessage('选择模式失败');
end;
end
else
begin
showmessage('sm is nil');
end;
end
else
showmessage('串口打开失败!');
end
else
showmessage('oc is nil');
end
else
showmessage('222');
FreeLibrary(DLLHandle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;


end.

改造完毕,读取二代证结果正常。
但是运行完毕
显示

不知道怎么做了?

谢谢楼上的兄弟姐妹们。
gxgyj 2013-06-28
  • 打赏
  • 举报
回复
意思是你声明的方式不对,参照楼上的来声明
默__ 2013-06-28
  • 打赏
  • 举报
回复
4楼的程序俺看不懂,请大侠指点一下,谢谢。
BlueEnchanter 2013-06-28
  • 打赏
  • 举报
回复
4楼的方法应该可以。出错的原因应该是你没分配内存空间,可以用4楼这种静态分配好的,或者程序里动态分配内存
火龙岛主 2013-06-28
  • 打赏
  • 举报
回复
直接声明:
var
    sfzName,sfzSex,sfzNation,sfzBirthday,sfzAddress,sfzID,sfzDepartment,
    sfzStartDate,sfzEndDate,sfzReserve,sfzAppAddress1,sfzPhotoData:pchar;
这个地方: ReadCard = function (var Name:pChar;var Sex:pChar;var Nation:pChar;var Birthday:pChar; var Address:pChar;var ID:pChar;var Department:pChar;var StartDate:pChar;var EndDate:pChar; var Reserve:pChar;var AppAddress1:pChar;var PhotoData:pChar):integer;stdcall;
gxgyj 2013-06-28
  • 打赏
  • 举报
回复
定义sfzName等参照如下,试试 我的方法

function ReadBaseInfosPhoto(Name,Sex,Folk,BirthDay,ID,Address,Issue,dBegin,dEnd,Photopath:PAnsiChar):integer;stdcall;External '.\dll\sdtapi.dll';


type
  TIDCardInfo=record
    _Name:array[1..31] of AnsiChar;
    _Sex:array[1..3] of AnsiChar;
    _Folk:array[1..10] of AnsiChar;
    _Birthday:array[1..9] of AnsiChar;
    _Addr:array[1..71] of AnsiChar;
    _ID:array[1..19] of AnsiChar;
    _Issue:array[1..31] of AnsiChar;
    _dBegin:array[1..9] of AnsiChar;
    _dEnd:array[1..9] of AnsiChar;
  private
    Fport:Integer;         //端口状态
    Fauthenticate:Integer; //认证状态
    Freadinfo:Integer;     //读卡状态
  end;


ReadCard.Freadinfo:=ReadBaseInfosPhoto(@ReadCard._Name,@ReadCard._Sex,@ReadCard._Folk,@ReadCard._Birthday,@ReadCard._ID,@ReadCard._Addr,@ReadCard._Issue,@ReadCard._dBegin,@ReadCard._dEnd,PAnsiChar(AnsiString(Fphotopath)));
默__ 2013-06-28
  • 打赏
  • 举报
回复
引用 2 楼 feiba7288 的回复:
试下修改成如下: if(rc(PChar(sfzName),PChar(sfzSex),PChar(sfzNation),PChar(sfzBirthday),PChar(sfzAddress),PChar(sfzID),PChar(sfzDepartment), PChar(sfzStartDate),PChar(sfzEndDate),PChar(sfzReserve),PChar(sfzAppAddress1),PChar(sfzPhotoData))=0)then
按照上面提示修改。现在提示 Project Project1.exe raised exception class EaccessViolation with message 'Access violation at address 1000511B in module 'JCJP_AP'.Write of address 00404311' Process stopped.Use Step or Run to continue.
feiba7288 2013-06-28
  • 打赏
  • 举报
回复
试下修改成如下: if(rc(PChar(sfzName),PChar(sfzSex),PChar(sfzNation),PChar(sfzBirthday),PChar(sfzAddress),PChar(sfzID),PChar(sfzDepartment), PChar(sfzStartDate),PChar(sfzEndDate),PChar(sfzReserve),PChar(sfzAppAddress1),PChar(sfzPhotoData))=0)then
默__ 2013-06-28
  • 打赏
  • 举报
回复

运行后错误提示。
神马豆是浮云 2013-06-28
  • 打赏
  • 举报
回复
我也曾经碰到过这个问题,运行时设置兼容模式即可。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧