调用C的DLL报内存错误,求助 ( 积分: 100 )

  • 主题发起人 主题发起人 cyfflying
  • 开始时间 开始时间
C

cyfflying

Unregistered / Unconfirmed
GUEST, unregistred user!
我调用东软的DLL,出现了问题
C中的函数原型
int cmpp_recv(conn_desc *conn,cmppe_packet *cp, unsigned char is_break,unsigned int *seq);

conn_desc结构如下
struct conn_desc{
SOCKET sock;
unsigned long seq;
int status;
char act_flag;
};

cmppe_packet结构如下
struct cmppe_packet{
cmppe_head pk_head;
signed int result;
union{
0 pk_deliver;
1 pk_login_resp;
2 pk_submit_resp;
}pk_data;
};

cmppe_head结构如下
struct cmppe_head{
unsigned int pk_len;
unsigned int pk_cmd;
unsigned int pk_stat;
unsigned int pk_seq;
};

东软自带的DEMO中调用代码如下
main()
{
conn_desc conn;
cmppe_packet cp;
memset(&conn,0,sizeof(conn));
memset(&cp,0,sizeof(cmppe_packet));
stat = cmpp_recv(&conn,&cp,0);
}

小弟定义的代码如下
type
Tconn = packed record
sock:TSocket;
seq:longint;
status:integer;
act_flag:pchar;
end;

Tcmpp_packet=packed record
head:Tcmpp_head;
result:longword;
case integer of
0:(pk_delive:byte);
1:(pk_login:byte);
2:(pk_submit_resp:byte);
end;

Tcmpp_head=packed record
pk_len:longword;
pk_cmd:longword;
pk_stat:longword;
pk_seq:longword;
end;
function cmpp_recv(conn:Pointer;Tcmpp_packet:Pointer;is_break:byte):integer;Cdecl;
implementation
function cmpp_recv;external 'api.dll' name 'cmpp_recv';
end.
调用的代码如下
var
login:^Tlogin;
head:^Tcmpp_packet;
begin
getMem(conn,sizeof(Tconn));
ZeroMemory(conn,sizeof(Tconn));
getMem(head,sizeof(Tcmpp_packet));
ZeroMemory(head,sizeof(Tcmpp_packet));
cmpp_recv(conn,head,0);
end;

运行后报内存错误,小弟对C实在是生疏,请各位大侠指点
 
我调用东软的DLL,出现了问题
C中的函数原型
int cmpp_recv(conn_desc *conn,cmppe_packet *cp, unsigned char is_break,unsigned int *seq);

conn_desc结构如下
struct conn_desc{
SOCKET sock;
unsigned long seq;
int status;
char act_flag;
};

cmppe_packet结构如下
struct cmppe_packet{
cmppe_head pk_head;
signed int result;
union{
0 pk_deliver;
1 pk_login_resp;
2 pk_submit_resp;
}pk_data;
};

cmppe_head结构如下
struct cmppe_head{
unsigned int pk_len;
unsigned int pk_cmd;
unsigned int pk_stat;
unsigned int pk_seq;
};

东软自带的DEMO中调用代码如下
main()
{
conn_desc conn;
cmppe_packet cp;
memset(&conn,0,sizeof(conn));
memset(&cp,0,sizeof(cmppe_packet));
stat = cmpp_recv(&conn,&cp,0);
}

小弟定义的代码如下
type
Tconn = packed record
sock:TSocket;
seq:longint;
status:integer;
act_flag:pchar;
end;

Tcmpp_packet=packed record
head:Tcmpp_head;
result:longword;
case integer of
0:(pk_delive:byte);
1:(pk_login:byte);
2:(pk_submit_resp:byte);
end;

Tcmpp_head=packed record
pk_len:longword;
pk_cmd:longword;
pk_stat:longword;
pk_seq:longword;
end;
function cmpp_recv(conn:Pointer;Tcmpp_packet:Pointer;is_break:byte):integer;Cdecl;
implementation
function cmpp_recv;external 'api.dll' name 'cmpp_recv';
end.
调用的代码如下
var
login:^Tlogin;
head:^Tcmpp_packet;
begin
getMem(conn,sizeof(Tconn));
ZeroMemory(conn,sizeof(Tconn));
getMem(head,sizeof(Tcmpp_packet));
ZeroMemory(head,sizeof(Tcmpp_packet));
cmpp_recv(conn,head,0);
end;

运行后报内存错误,小弟对C实在是生疏,请各位大侠指点
 
function cmpp_recv(conn:Pointer;Tcmpp_packet:Pointer;is_break:byte):integer;Cdecl;
换成:
function cmpp_recv(conn:Pointer;Tcmpp_packet:Pointer;is_break:byte):integer;stdcall;
试试看!
 
谢谢,试过了,效果一样
现在是这样的情况
如果我执行了这句后,什么操作都不做,暂时没出错信息,关闭程序时会报invalid pointer opeation,如果我读取返回值什么的,就直接报内存错误
 
Pcmpp_packet = ^Tcmpp_packet;
Tcmpp_packet=packed record
head:Tcmpp_head;
result:longword;
case integer of
0:(pk_delive:byte);
1:(pk_login:byte);
2:(pk_submit_resp:byte);
end;

function cmpp_recv(conn:Pointer;Pcmpp_packet:Pointer;is_break:byte):integer;stdcall;
 
谢谢,内存报错没了
但invalid pointer opeation这个错误信息还一直在
 
Tconn = packed record
sock:integer;
seq:longint;
status:integer;
act_flag:pchar;
end;
C中的SOCKET不是DELPHI中的TSOCKET
你最好查一下文档
还有其他对应的类型
 
int cmpp_recv(conn_desc *conn,cmppe_packet *cp, unsigned char is_break,unsigned int *seq);
function cmpp_recv(conn:Pointer;Tcmpp_packet:Pointer;is_break:byte):integer;Cdecl;
参数数目怎么不一样?seq这个参数到底有没有?
type
Tconn = packed record
sock:TSocket;
seq:cardinal;
status:integer;
act_flag:char;
end;

Tcmpp_packet=packed record
head:Tcmpp_head;
result:integer;
case integer of
0:(pk_delive:byte);
1:(pk_login:byte);
2:(pk_submit_resp:byte);
end;

Tcmpp_head=packed record
pk_len:cardinal;
pk_cmd:cardinal;
pk_stat:cardinal;
pk_seq:cardinal;
end;
function cmpp_recv(var conn:Tconn_desc;var Tcmpp_packet:Tcmppe_packet;is_break:byte):integer;Cdecl;
调用:
var
conn:Tconn;
head:Tcmpp_packet;
begin
ZeroMemory(@conn,sizeof(Tconn));
ZeroMemory(@head,sizeof(Tcmpp_packet));
cmpp_recv(conn,head,0);
end;
 
不好意思,忘了说明了,这个seq是可以没有的
 
我这么觉得不应该用Packed呢,普通record试过没
 
楼上的看来没有做过混合编程
在C中STRUCT对应的就是DELPHI的PACKED RECORD 这点是绝对没错的
 
呵呵,小弟刚开始学编程,确实没做过,学习学习

在下也只是提醒楼主是否有这方面原因,本无他意
 
delphi调用c好象是老生常谈的问题了。是否函数声明应该写成:
function cmpp_recv(var conn:Pointer;var Tcmpp_packet:Pointer;const is_break:byte):integer;stdcall;
 
小弟水平有限,怎么调试都不行
可是东西又挺急,哪位大虾愿帮一把,请留个QQ,MSN啥的,分数可以另开,小弟感激不尽
 
留下你的QQ号,
 
我的QQ:4290560
 
后退
顶部