C如何回调C++的函数?如果回调函数中有C有C++调用其他C++函数的代码该如何?
有个C库clib.a要给php调用,并且给C库一个回调函数,因为php无法直接调用C库,所以我用第三方库C++库,PHP-CPP给php写了一个扩展
(C库回调PHP函数确实是很蛋疼的需求,但是没办法,需求在那里也只能想办法满足了)
C++库中调用C库的函数myComm:
Php::Value InitComm(Php::Parameters ¶meters)
{
SERVERINFO_PARAM ServerInfoParam[1]=
{
{0,"127.0.0.1",6000,callback_for_c}
};
relt = myComm(ServerInfoParam);
return relt;
}
myComm说明:
myComm(ServerInfoParam);
ServerInfoParam是个结构体,
typedef ubool (*FN_HANDLER) (int32 lIdentifyCode,int16 nMsgType,void *pstMsg,int32 lMsgLen);
typedef struct tagSERVERINFO
{
int32 lServerID;
int8 pcServerIpAddr[64];
int32 lServerPort;
FN_HANDLER fnMsgHandler; //函数指针
}SERVERINFO_PARAM;
要给C库一个回调函数,C库无法直接调用C++函数,所以我写了一个callback_for_c函数,按C编译给函数指针fnMsgHandler用
extern "C" {
int32 callback_for_c(int32 lIdentifyCode,int16 nMsgType,void *pstMsg,int32 lMsgLen)
{
Php::Value relt;
printf(“call callback_for_c sucess!\n");
// call a function from user space
Php::Value data = Php::call("some_function","some_parameter");
return int32_t(relt);
}
}
callback_for_c函数通过PHP-CPP库提供的函数Php::call回调PHP的函数,目的是想通过这样的方式(C库回调callback_for_c,callback_for_c中调Php::call,Php::call调some_function)
达到C库回调php函数的目的。
php中的函数:
// define a user space function
//这个函数会放在php端,由PHP人员自己往里面添加自己的函数代码
function some_function($param)
{
//***
**//
echo("userspace function called with $param\n");
}
但是运行函数时,函数在运行到Php::Value data = Php::call("some_function","some_parameter"); 段错误。
gdb信息如下:
(gdb) bt
#0 0x006c19f0 in _emalloc ()
#1 0xb6f57374 in zend_string_alloc (persistent=0, len=13) at /usr/include/php-zts/php/Zend/zend_string.h:122
#2 zend_string_init (persistent=0, len=13, str=0xb6ff0181 "some_function") at /usr/include/php-zts/php/Zend/zend_string.h:158
#3 Php::Value::Value (this=0xb4f4fdd0, value=0xb6ff0181 "some_function", size=-1) at zend/value.cpp:121
#4 0xb6fdbace in callback_for_c () from /usr/lib/php-zts/modules/clientcomm.so
#5 0xb6fe2de3 in RecvCommMultiFrameFinishProcess (lCommModleID=1, ulIdentifyCode=1, unMsgType=62, pstDataBuff=0xb44ff01c "\023",
lDataLen=124) at clientsrv.c:161
#6 0xb6fe2937 in ServerSingleFrameProcess (lModuleID=1, pstFrameHead=0xb44ff010 "\237", <incomplete sequence \371\204>,
pMsgBuff=0xb44ff01c, lMsgLen=124) at clientsprot.c:63
#7 0xb6fe2b2e in ClientFrameProcess (_this=0xb4600610, pFrameHead=0xb44ff010 "\237", <incomplete sequence \371\204>, lFrameLen=136,
lFrameType=3) at clientsprot.c:142
#8 0xb6fef653 in ParserProcessData (_this=0xb4600610, pstParser=0xb6ff4d80 <s_stHwntpProtParser>,
fnProcFrame=0xb6fe2a1f <ClientFrameProcess>) at ../src/mngrtutil.c:158
#9 0xb6fe23a4 in TryClientRecvProcess (_this=0xb4600610) at clientcomm.c:119
#10 0xb6fe27f4 in ClientOnTimer (_this=0xb4600610) at clientcomm.c:365
#11 0xb6fee560 in TryProcEachModule (pstModuleMgr=0xb46005d0) at ../src/modulemgr.c:439
#12 0xb6fee66c in ExecModuleMgr (pstModuleMgr=0xb46005d0) at ../src/modulemgr.c:498
#13 0xb6fdf20b in ExecModules (lServerNum=1) at hwmain.c:171
#14 0xb6fdf24a in hw_main (argc=1, argv=0x0) at hwmain.c:194
#15 0xb6fde7d5 in HwCommModuleManager (arg=0xb6ff4bc0 <s_astFunctionParam>) at hwclientcomm.c:164
#16 0xb7aed577 in start_thread () from /lib/libpthread.so.0
#17 0xb746c076 in clone () from /lib/libc.so.6
(gdb)
是不是因为虽然我把函数Php::call("some_function","some_parameter")放在callback_for_c里面并且编译通过,但是在调用call和call调用some_function
都是C库中执行,也就是目标库执行,而目标库是按C的方式执行,而some_function函数是通过PHP-CPP库转成C++函数的,所以C库一旦调用some_function就出错?
有什么办法能解决我的这个问题吗?
PHP-CPP库的资料和源代码在这里可以获取:
http://www.php-cpp.com/
下面是它的例子:
http://www.php-cpp.com/documentation/calling-functions-and-methods