有点难度:如何处理传入的回调函数问题

bxjgood66 2006-09-28 05:31:45
有一个函数:
Startup( char* cLocalIp,
short iLocalPort,
char* cSmgIp,
short iSmgPort,
char* cLoginName,
char* cPassword,
int lSocketTimeOut,
int lMTTimeOut,
unsigned int iSrcNode,
int lMTThread,
int lMaxListNode,
int lMTMaxRetryTime,
int lMOThread,
void (*fProcessDeliver)(DeliverStr deliver),
void (*fProcessReport)(ReportStr report),
void (*fProcessResponse)(MTRespStr response),
void (*ProcessMTError)(MTErrorStr mterror)
);

void (*fProcessDeliver)(DeliverStr deliver);
void (*fProcessReport)(ReportStr report);
void (*fProcessResponse)(MTRespStr response);

我的目的是在调用:Startup(...void.. ,void.., void..)传入这三个回调函数,但是我不知道如何来写这三个函数?如何把我的处理写入这三个函数内部?这些语法我不太懂,我不知道void (*fProcessDeliver)(DeliverStr deliver);这样写是什么意思? 麻烦各位兄弟给我讲解一下,或者示范一下,谢谢各位了。
...全文
167 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sczyq 2006-09-29
  • 打赏
  • 举报
回复
.h文件中回调函数声明
private:
void __fastcall fProcessDeliver(DeliverStr deliver);
void __fastcall fProcessReport(ReportStr report);
void __fastcall fProcessResponse(MTRespStr response);

.cpp文件中实现函数
void __fastcall TMainFrm::fProcessDeliver(DeliverStr deliver)//MainFrm是你的窗体名称
{
...
}
void __fastcall TMainFrm::fProcessReport(ReportStr report)
{
...
}
void __fastcall TMainFrm::fProcessResponse(MTRespStr response)
{
...
}

在函数:
Startup( char* cLocalIp,
short iLocalPort,
char* cSmgIp,
short iSmgPort,
char* cLoginName,
char* cPassword,
int lSocketTimeOut,
int lMTTimeOut,
unsigned int iSrcNode,
int lMTThread,
int lMaxListNode,
int lMTMaxRetryTime,
int lMOThread,
void __fastcall (__closure *fpProcessDeliver)(DeliverStr),
void __fastcall (__closure *fpProcessReport)(ReportStr),
void __fastcall (__closure *fpProcessResponse)(MTRespStr),
void (*ProcessMTError)(MTErrorStr mterror)
);
i_love_pc 2006-09-29
  • 打赏
  • 举报
回复
.h文件中回调函数声明
private:
static void CALLBACK fProcessDeliver(DeliverStr deliver);
static void CALLBACK fProcessReport(ReportStr report);
static void CALLBACK fProcessResponse(MTRespStr response);

.cpp文件中实现函数
void CALLBACK TMainFrm::fProcessDeliver(DeliverStr deliver)//MainFrm是你的窗体名称
{
...
}
void CALLBACK TMainFrm::fProcessReport(ReportStr report)
{
...
}
void CALLBACK TMainFrm::fProcessResponse(MTRespStr response)
{
...
}

在函数:
Startup( char* cLocalIp,
short iLocalPort,
char* cSmgIp,
short iSmgPort,
char* cLoginName,
char* cPassword,
int lSocketTimeOut,
int lMTTimeOut,
unsigned int iSrcNode,
int lMTThread,
int lMaxListNode,
int lMTMaxRetryTime,
int lMOThread,
fProcessDeliver,
fProcessReport,
fProcessRespons,
void (*ProcessMTError)(MTErrorStr mterror)
);
BlueDeepOcean 2006-09-29
  • 打赏
  • 举报
回复
回调用过程简单地说就如同对变量的调用一样,因此,完全可以将函数名当作变量名对待。
bxjgood66 2006-09-29
  • 打赏
  • 举报
回复
谢谢 上面各位,按keiy() 的方法解决了
sczyq 2006-09-28
  • 打赏
  • 举报
回复
回调函数在类内部的,要加上__closure 还有 __fastcall


有时编译器不支持函数地址直接使用问题,则需要先变成函数指针
柯本 2006-09-28
  • 打赏
  • 举报
回复
void (*fProcessDeliver)(DeliverStr deliver);
中的fProcessDeliver为指向一个void xxx(DeliverStr deliver);函数的指针,很多c的书上有介绍
你函数可以这样用,先定义4个回调函数:

void test1(DeliverStr deliver)
{
//处理程序
}
void test2(ReportStr report)
{
//处理程序
}
void test3(MTRespStr response)
{
//处理程序
}
void test4(MTErrorStr mterror)
{
//处理程序
}
然后用类似(参数我不清楚,全设为0)
Startup(0,0,0,0,0,0,0,0,0,0,0,0,0,test1,test2,test3,test4);
在Startup执行时,会回调你的test1-4函数
bxjgood66 2006-09-28
  • 打赏
  • 举报
回复
自己顶一下哟
bxjgood66 2006-09-28
  • 打赏
  • 举报
回复
自己顶一下哟
bxjgood66 2006-09-28
  • 打赏
  • 举报
回复
有人关注吗?谢谢

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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