testUpdate回调按值接受double,而不是指针,两个回调都返回void,即None。在
candleNumber_c是不必要的,尤其是当您声明argtypes时。这同样适用于historyFilePath_c。Python字符串已经包含指向以null结尾的字符串的指针,c_char_p构造函数使用该指针。在
为什么要将error_c初始化到地址300?在
下面是一个有帮助的例子:
临时股东大会:from ctypes import *
lib = CDLL('./tmp.so')
c_test_update_t = CFUNCTYPE(None, c_double)
c_test_finished_t = CFUNCTYPE(None)
run_test = lib.runTest
run_test.argtypes = [
c_char_p, c_uint, c_test_update_t, c_test_finished_t,
POINTER(c_char_p)]
def test_update(percent_completed):
print 'percent completed:', percent_completed
test_update_c = c_test_update_t(test_update)
def test_finished():
print 'test finished'
test_finished_c = c_test_finished_t(test_finished)
error = c_char_p()
result = run_test(
'path/to/history', 1000, test_update_c, test_finished_c,
byref(error))
print result
print error.value
tmp.c:
^{pr2}$
(只是一个简单的测试库,在Linux上用gcc编译)
输出:
^{3}$