请教Delphi 调用 c的DLL,参数传递问题

SaibeiStar 2009-04-16 10:33:02
有一动态库中一函数.h中的原型是
extern "C" __declspec(dllimport) short __stdcall gCPUCommand(stAPDU *APDU , char *SW, char *Data, char *pchErrMsg);

typedef struct
{
char cla[3];
char ins[3];
char p1[3];
char p2[3];
char p3[3];
char data[256];
}stAPDU;
他声明的是(DLL开发说明)
函数名 short gCPUCommand(char *APUD, char *SW, char *Data, char *pchErrMsg)


C++BUILDER中的调用方法如下:
stAPDU APDU;
char chErrMsg[256],SW[10],,Data[128];
short ret; memset(chErrMsg,'\0',256);
memset(Data,'\0',128); memset(SW,'\0',10);
memset(State,'\0',3);
strcpy( APDU.cla, '00'); strcpy( APDU.ins ,'00');
strcpy( APDU.p1 , '00'); strcpy( APDU.p2 , '00');
strcpy( APDU.p3 , '00'); strcpy( APDU.data , '00');
ret = gCPUCommand( (stAPDU *) &APDU,SW,Data,chErrMsg);//正常

我写的调用方法:
方法1
CommData:pchar;
CommData:=pchar('0084000008');
ulRet := gCPUCommand(CommData,SW,pbReData,pchErrMsg);//返回Command BCC Verify Error
方法2
type sApdu=packed record
cla: pchar ;
ins : pchar ;
p1: pchar ;
p2 : pchar ;
p3 : pchar ;
data : pchar ;
end;
PsendComm=^sapdu;

sendComm:PsendComm;
sendComm^.cla:=pchar(Copy(pbSendBuf,1,2));
sendComm^.ins :=pchar(Copy(pbSendBuf,3,2));
sendComm^.p1 :=pchar(Copy(pbSendBuf,5,2));
sendComm^.p2 :=pchar(Copy(pbSendBuf,7,2));
sendComm^.p3 :=pchar(Copy(pbSendBuf,9,2));
sendComm^.data :=pchar(Copy(pbSendBuf,11,length(pbSendBuf)));
ulRet := gCPUCommand(@sendComm,SW,pbReData,pchErrMsg);//返回Command BCC Verify Error

是不是参数传错了?



...全文
178 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
SaibeiStar 2009-04-20
  • 打赏
  • 举报
回复
ret = gCPUCommand( (stAPDU *) &APDU,SW,Data,chErrMsg);c++builder的这种调用方法与Delphi中
ulRet := gCPUCommand(@sendComm,SW,pbReData,pchErrMsg);的调用方法不是一样吗?难道DELHI中的参数还是不对?
SaibeiStar 2009-04-20
  • 打赏
  • 举报
回复
还是不行,估计还是参数传的不对
c++builder 这样传就没问题:
strcpy( APDU.cla ,Edit6->Text.c_str());
strcpy( APDU.ins ,Edit7->Text.c_str());
strcpy( APDU.p1 , Edit8->Text.c_str());
strcpy( APDU.p2 , Edit9->Text.c_str());
strcpy( APDU.p3 , Edit10->Text.c_str());
strcpy( APDU.data , Edit11->Text.c_str());
StatusBar1->Panels->Items[1]->Text = "Current Operate:Common Command For CPU";

ret = gCPUCommand( (stAPDU *) &APDU,SW,Data,chErrMsg);
xiaoxiao_8 2009-04-20
  • 打赏
  • 举报
回复
type
stAPDU = packed record
cla :array[0..3-1] of AnsiChar;
ins:array[0..3-1] of AnsiChar;
p1:array[0..3-1] of AnsiChar;
p2:array[0..3-1] of AnsiChar;
p3:array[0..3-1] of AnsiChar;
data:array[0..256-1] of AnsiChar;
end;
僵哥 2009-04-20
  • 打赏
  • 举报
回复
function gCPUCommand(APDU :LPAPDU; SW, Data, pchErrMsg: AnsiChar): smallint;stdcall; external *.dll;这个自己写
僵哥 2009-04-20
  • 打赏
  • 举报
回复
type
stAPDU = record
cla :array[0..3-1] of AnsiChar;
ins:array[0..3-1] of AnsiChar;
p1:array[0..3-1] of AnsiChar;
p2:array[0..3-1] of AnsiChar;
p3:array[0..3-1] of AnsiChar;
data:array[0..256-1] of AnsiChar;
end;
LPAPDU = ^stAPDU;
function gCPUCommand(APDU :LPAPDU; SW, Data, pchErrMsg: AnsiChar): smallint;stdcall;

var
APDU:stAPDU;
chErrMsg:array[0..256-1] of AnsiChar;
SW:array[0..10-1] of AnsiChar
Data: array[0..128-1] of AnsiChar;
ret: SmallInt;
begin
fillchar(chErrMsg,256,0);
fillchar(Data,128,0);
fillchar(SW,10,0);
fillchar(State,3,0);
strpcpy( APDU.cla, '00'); strpcpy( APDU.ins ,'00');
strpcpy( APDU.p1 , '00'); strpcpy( APDU.p2 , '00');
strpcpy( APDU.p3 , '00'); strpcpy( APDU.data , '00');
ret := gCPUCommand( @APDU,SW,Data,chErrMsg);


SaibeiStar 2009-04-20
  • 打赏
  • 举报
回复
有知道的吗?
starluck 2009-04-16
  • 打赏
  • 举报
回复
StrCopy
SaibeiStar 2009-04-16
  • 打赏
  • 举报
回复
TO starluc

ulRet := gCPUCommand(sendComm,SW,pbReData,pchErrMsg);//返回Command BCC Verify Error
参数传sendComm能执行?

改成cla: array[0..2] of char;
赋值
strlcopy(@sendComm^.cla,pchar(Copy(pbSendBuf,1,2)),sizeof(sendComm^.cla)-1);

执行还是不正确
SaibeiStar 2009-04-16
  • 打赏
  • 举报
回复
我原来也是定义成cla: array[0..2] of char;
这样怎么赋值呢?sendComm^.cla:=pchar(Copy(pbSendBuf,1,2)); ?
SaibeiStar 2009-04-16
  • 打赏
  • 举报
回复
我是这样定义的 function gCPUCommand(APUD:PChar;SW:PChar;Data:PChar;pchErrMsg:PChar ):integer;stdcall; external '***.dll';
starluck 2009-04-16
  • 打赏
  • 举报
回复

typedef struct
{
char cla[3];
char ins[3];
char p1[3];
char p2[3];
char p3[3];
char data[256];
}stAPDU;



//搞個對齊做什麼??


type sApdu =record
cla: array[0..2] of char;
ins : array[0..2] of char;
p1: array[0..2] of char ;
p2 : array[0..2] of char ;
p3 : arraya[0..2] of char ;
data : array[0..255] of char;
end;

PsendComm=^sapdu;

sendComm:PsendComm;
sendComm^.cla:=pchar(Copy(pbSendBuf,1,2));
sendComm^.ins :=pchar(Copy(pbSendBuf,3,2));
sendComm^.p1 :=pchar(Copy(pbSendBuf,5,2));
sendComm^.p2 :=pchar(Copy(pbSendBuf,7,2));
sendComm^.p3 :=pchar(Copy(pbSendBuf,9,2));
sendComm^.data :=pchar(Copy(pbSendBuf,11,length(pbSendBuf)));
ulRet := gCPUCommand(sendComm,SW,pbReData,pchErrMsg);//返回Command BCC Verify Error


16,748

社区成员

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

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