我的Python代码中,有一个C的回调函数:
global cbGetIMSI
CB_GET_IMSI = CFUNCTYPE(None, c_char_p)
cbGetIMSI = CB_GET_IMSI(py_getIMSI)
def py_getIMSI(imsi):
global tc
tc = import(mod)
imsi = tc.getIMSI()
print '####### imsi = ' + imsi
并调用C函数进行注册:
lib_inf = cdll.LoadLibrary(self.libinf.get())
lib_inf.inf_regCbGetImsi(cbGetIMSI)
我的C代码:
typedef void (*inf_PyCbGetImsi)(char *);
int inf_regCbGetImsi(inf_PyCbGetImsi cbFn)
{
DBG("enter [%s()]", FUNCTION);
if (!cbFn)
{
return -1;
}
g_pyCB.getImsi = cbFn;
return 0;
}
static int GetIMSI()
{
int ret = 0;
int i = 0;
unsigned char aIMSI[15];
DBG("[enter %s()]", __FUNCTION__);
memset(aIMSI, 0, sizeof(aIMSI));
if (g_pyCB.getImsi)
{
g_pyCB.getImsi(aIMSI);
}
DBG("[%s()] aIMSI = [%s]", __FUNCTION__, aIMSI);
return ret;
}
在Python中运行日志:
####### imsi = 001010123456789
in C Program:
[GetIMSI()] aIMSI = []
为什么参数的值没有改变,我该如何修复它?谢谢!你知道吗
问题出在你身上:
这里不需要设置
imsi
的值,只需将它重新定义为另一个“something”,这也是在C中的行为。你知道吗如果您想设置
imsi
的值,您应该使用strcpy
之类的方法复制它中的值,这可以通过加载libc
和使用libc.strcpy(imsi, tc.getIMSI())
之类的方法来使用参考:Using ctypes to write callbacks that pass-in pointer to pointer parameters to be used as output-parameter
定义如下:CFUNCTYPE(c\u byte,POINTER(POINTER(c\u char)))
相关问题 更多 >
编程相关推荐