将c文件生成动态库:gcc test.c -fPIC -shared -o libtest.so
import ctypes
test = ctypes.CDLL("./libtest.so")
s1 = '0123456789'
s2 = '0123456789'
s3 = '987654321'
print 'strcmp case:'
print test.strcmpTest(s1, s2)
print 'strcpy case:'
test.strcpyTest(s1, s3)
print s1
print 'readStr case:'
print test.readStr(s3)
执行结果如下:
strcmp case:
0
strcpy case:
987654321
readStr case:
LibPrint: 987654321 addr=0x7fb00c1e0fb4
203296692
我用计算器算了一下,203296692 == 0xc1e0fb4(32bits)。我实验的机器是64bits,所以看起来addr要大。并且readStr的返回值到python脚本中,是以32bits有符号存储的。