C结构体
struct ICCCommand
{
unsigned int function; /* Function (see function table ) */
unsigned int efIdent; /* short EF-Identifier */
unsigned int dataPointer; /* file- offset / record number */
unsigned int lc; /* length send data */
unsigned char * data; /* send datablock */
unsigned int le; /* max length response */
};
struct ICCResponse
{
unsigned char sw1; /* Status bytes */
unsigned char sw2; /* */
unsigned int ldata; /* length response */
unsigned char * data; /* respons datablock */
};
struct ICCSecurity
{
unsigned char macDown; /* signature algorithm */
unsigned char macUp; /* */
unsigned char * macKey; /* signature key */ unsigned char encDown; /* encryption algorithm */
unsigned char encUp; /* */
unsigned char * encKey; /* encryption key */
};
C动态库int ICCCreateFile (struct ICCCommand * com,
struct ICCResponse * res,
struct ICCSecurity * sec,
int ch,int mode
)
delphi结构体
PICCCommand=^ICCCommand1;
PICCResponse=^ICCResponse1;
PICCSecurity=^ICCSecurity1;
ICCResponse1 = packed record
sw1:byte;
sw2:byte;
ldata:LongWord;
data:PChar;
end; ICCCommand1 = packed record
func:LongWord;
efident:LongWord;
datapointer:LongWord;
lc:LongWord;
data:PChar;
le:LongWord;
end ; ICCSecurity1 = packed record
macdown:byte;
macup:byte;
mackey:pchar;
encdown:byte;
encup:byte;
enckey:pchar;
end ;
delphi调用
function ICCCreateFile1(var command:PICCcommand; var response:PICCresponse;var security:PICCSecurity;handle_IC,t:LongInt):LongInt;external 'cardos32.dll' name 'ICCCreateFile'; New(ic_command1);
New(ic_reutrn1);
New(ic_security1);
tryic_command1.func := 63 ;
ic_command1.efIdent := 0 ;
ic_command1.datapointer := 0 ; s1 := '0000000038ffffff0000';
ic_command1.data := StrNew(PChar(S1));//ic_command1.data := P2;//'0000000038ffffff0000' ;
ic_command1.lc := 10 ;
ic_command1.le := 300 ;
s := space(300);
ic_reutrn1.data := StrNew(PChar(S));
// StrCopy(ic_reutrn1.data,@s);
// ic_reutrn1.data := space(300) ;
ic_security1.encDown := 0 ;
ic_security1.encUp := 0 ;
ic_security1.macDown := 0 ;
ic_security1.macUp := 0 ; if ICCCreateFile1(ic_command1,ic_reutrn1,ic_security1,handle_ic,1) = 0 then
这一句一调用就报地址错误,各位帮忙看一下什么原因呢,谢谢
struct ICCCommand
{
unsigned int function; /* Function (see function table ) */
unsigned int efIdent; /* short EF-Identifier */
unsigned int dataPointer; /* file- offset / record number */
unsigned int lc; /* length send data */
unsigned char * data; /* send datablock */
unsigned int le; /* max length response */
};
struct ICCResponse
{
unsigned char sw1; /* Status bytes */
unsigned char sw2; /* */
unsigned int ldata; /* length response */
unsigned char * data; /* respons datablock */
};
struct ICCSecurity
{
unsigned char macDown; /* signature algorithm */
unsigned char macUp; /* */
unsigned char * macKey; /* signature key */ unsigned char encDown; /* encryption algorithm */
unsigned char encUp; /* */
unsigned char * encKey; /* encryption key */
};
C动态库int ICCCreateFile (struct ICCCommand * com,
struct ICCResponse * res,
struct ICCSecurity * sec,
int ch,int mode
)
delphi结构体
PICCCommand=^ICCCommand1;
PICCResponse=^ICCResponse1;
PICCSecurity=^ICCSecurity1;
ICCResponse1 = packed record
sw1:byte;
sw2:byte;
ldata:LongWord;
data:PChar;
end; ICCCommand1 = packed record
func:LongWord;
efident:LongWord;
datapointer:LongWord;
lc:LongWord;
data:PChar;
le:LongWord;
end ; ICCSecurity1 = packed record
macdown:byte;
macup:byte;
mackey:pchar;
encdown:byte;
encup:byte;
enckey:pchar;
end ;
delphi调用
function ICCCreateFile1(var command:PICCcommand; var response:PICCresponse;var security:PICCSecurity;handle_IC,t:LongInt):LongInt;external 'cardos32.dll' name 'ICCCreateFile'; New(ic_command1);
New(ic_reutrn1);
New(ic_security1);
tryic_command1.func := 63 ;
ic_command1.efIdent := 0 ;
ic_command1.datapointer := 0 ; s1 := '0000000038ffffff0000';
ic_command1.data := StrNew(PChar(S1));//ic_command1.data := P2;//'0000000038ffffff0000' ;
ic_command1.lc := 10 ;
ic_command1.le := 300 ;
s := space(300);
ic_reutrn1.data := StrNew(PChar(S));
// StrCopy(ic_reutrn1.data,@s);
// ic_reutrn1.data := space(300) ;
ic_security1.encDown := 0 ;
ic_security1.encUp := 0 ;
ic_security1.macDown := 0 ;
ic_security1.macUp := 0 ; if ICCCreateFile1(ic_command1,ic_reutrn1,ic_security1,handle_ic,1) = 0 then
这一句一调用就报地址错误,各位帮忙看一下什么原因呢,谢谢
解决方案 »
- form.free、from.close、form:=nil 有什么区别
- 如何实现通过串口控制手机的拨号与拨通?
- DBGrid中的標題titleclick時出錯?
- 如下的代码为何总是提示‘update’语句错???!!!
- 菜鸟遇到的题目:设计一个虚拟数字示波器。(急求~~~)
- delphi 中的消息传送。。。(100分求解)
- 求:谁有立方体旋转的程序?需要源代码!谢谢,万分焦急!!!谢谢,100分送!!!
- 用FindResource(DllHandle, PChar(ResName), RT_ICON) 为什么找不到资源文件?
- 请问那里有关于delphi(pascal)函数的电子书可以下载?(
- 怎样使鼠标放在Label上时,字体变色?我是低手,请赐教.谢谢
- 怎样检测一篇UTF8编码的网页中的中文是否为繁体?
- 如何在delphi7中直接调用IE打开对应网址?
因为C写的没有编译环境,在它里面看不到传进去的参数值,在没有调用之前这些个是有赋值的
2. 函数翻译:一个注意调用约定,按照你给的c声明应该是 cdecl
要么
function ICCCreateFile1(var command: ICCcommand; var response: ICCresponse; var security: ICCSecurity;handle_IC,t:LongInt):LongInt; cdecl;
要么
function ICCCreateFile1(command: PICCcommand; response: PICCresponse;security: PICCSecurity; handle_IC,t:LongInt):LongInt;external 'cardos32.dll' name 'ICCCreateFile'; cdecl;不要同时用 var 和指针声明,根本对不上
谢谢你,这个函数已经可以调用了,另外我想问一下
var
ls_return:string;
ic_command1:PICCCommand;
ic_reutrn1:PICCResponse;
ic_security1:PICCSecurity;
s,s1:string;
p1,p2:PChar;
begin
New(ic_command1);
New(ic_reutrn1);
New(ic_security1);
try
ic_command1.func := 63 ;
ic_command1.efIdent := 0 ;
ic_command1.datapointer := 0 ;
s1 := '0000000038ffffff0000';
ic_command1.data := StrNew(PChar(S1));
ic_command1.lc := 10 ;
ic_command1.le := 300 ;
s := space(300);
ic_reutrn1.data := StrNew(PChar(S));
ic_security1.encDown := 0 ;
ic_security1.encUp := 0 ;
ic_security1.macDown := 0 ;
ic_security1.macUp := 0 ; if ICCCreateFile1(ic_command1,ic_reutrn1,ic_security1,handle_ic,1) = 0 then
s:=''
else
s:='';
finally
// StrDispose(p1);
// StrDispose(p2);
// freemem(ic_security1);
// freemem(ic_reutrn1);
// freemem(ic_command1);
Dispose(ic_security1);
Dispose(ic_reutrn1);
Dispose(ic_command1);
end;
在释放的时候报地址错误,是不是用strnew创建的也要先释放,怎么释放呢
你申请了一个值为p1的空间,把地址赋给了ic_command1.data
还是Dispose(ic_security1);
都可以执行,但是运行到函数最后的时候报错(应该是离到函数体时),
函数体最后是释放Dispose(ic_security1)
对,在dll中是要对ic_reutrn1进行操作的,因为它是一个返回值
要怎么释放呢,难道释放不了?
struct ICCResponse * res,
struct ICCSecurity * sec,
int ch,int mode
)
{
int status, line,olen;
unsigned char data[200];
if(mode/2==0)
memcpy(data,com->data,com->lc);
else
AsciiToChar(data,com->data,com->lc);
if(mode<2)
{
InCommand( ch, CLASS, Create_File[CardOSVersion] , com->function*P1POWER,
(unsigned char)com->lc, data, &(res->sw1), &(res->sw2),
&status, &line,&olen,4,(unsigned char)com->le);
}
else
{
InCommand_echo( ch, CLASS, Create_File[CardOSVersion] , com->function*P1POWER,
(unsigned char)com->lc, data, &(res->sw1), &(res->sw2),
&status, &line,&olen,4,(unsigned char)com->le);
}
if(line!=0)
return NOSEND;
if(status==0)
{
res->ldata=(unsigned int)olen;
if(mode/2==0)
memcpy(res->data,data,olen); 在此处修改了
else
CharToAscii(res->data,data,olen);
}
else
res->ldata=0;
return status;
}
这是C这个函数的完整实现
ICCResponse1 = packed record
sw1:byte;
sw2:byte;
ldata:integer;
data:PChar;
end;ldata:integer;
试试。function ICCCreateFile1(var command:PICCcommand; var response:PICCresponse;var security:PICCSecurity;handle_IC,t:LongInt):LongInt;external 'cardos32.dll' name 'ICCCreateFile';
加上 stdcall 试试
这种方法不行,最开始就是这样,按Seamour 7楼的方法可以调用了
用你的方法调用报地址错误
懒得看你贴的代码了,八成是某个要传 char * 需要写,你不但没给分配内存,还传了个野指针
s1 := '0000000038ffffff0000';
ic_command1.data := StrNew(PChar(S1));
改成
s1 := '0000000038ffffff0000';
edit.Text := s1;
edit.GetTextBuf(strdata,1024);
ic_command1.data := strdata;//StrNew(PChar(S1));
但是gettextbuf要带个编辑控件吗?有没有等价的,是不是getmem呢?