在ARM平台上,python脚本使用ctypes方法调用C动态库,C动态库中void*类型的值返回到python中,地址被截断。代码如下:
// the interface of C library
// c.h
#ifdef __cplusplus
extern "C" {
#endif
#define _stdcall
void *_stdcall GetVoidPoint();
int _stdcall TestVoidPoint(void* pvPoint);
#ifdef __cplusplus
}
#endif
// the interface of C library
//c.cpp
#include <stdio.h>
#include <unistd.h>
#include "c.h"
class CMyClass
{
public:
CMyClass()
{
num1 = 5;
}
void PrintInfo()
{
printf("in dll, num1 = %d\n", num1);
}
private:
int num1;
};
void* _stdcall GetVoidPoint()
{
CMyClass *pMyClass = new CMyClass();
printf("in dll, GetVoidPoint, pMyClass = %p\n", pMyClass);
return pMyClass;
}
int _stdcall TestVoidPoint(void* pv)
{
printf("in dll, TestVoidPoint, pvPoint = %p\n", pv);
CMyClass *pTmp = (CMyClass*)pv;
pTmp->PrintInfo();
delete pTmp;
return 0;
}
#_*_ coding:cp936 _*_
from ctypes import *
import string
if __name__=="__main__":
test_Handle = cdll.LoadLibrary(r"libtest_c.so")
#set return type
#test_Handle.GetVoidPoint.restype = c_void_p
ret1 = test_Handle.GetVoidPoint()
print "ret1 = %#x" %(ret1)
#set argument type
#test_Handle.TestVoidPoint.argtypes = [c_void_p]
ret2 = test_Handle.TestVoidPoint(ret1)
print "ret2 = %d" %ret2
在ARM平台下,不设置返回值和参数类型,那么结果如下:
在ARM平台下,设置返回值和参数类型,那么结果如下:
原因分析:
(1)默认情况下,ctype的返回值的类型是int。python中int类型对应C中的int类型,c中int类型在64位机器上的是4个字节,占32位。ARM平台下,获取到的地址是12位16进制,总共48位,例如:0xaaadf0f0caca(其实是0x0000aaadf0f0caca,高四位用不到),如果python没有设置返回值是c_void_p,那么在void*转int类型时,返回值的位数被截断为32位,例如:0xf0f0caca。
(2)默认情况下,ctype的参数的类型是int。此时以0xf0f0caca的值传入C动态库,在动态库中的地址其实表示的0xfffffffff0f0caca,和之前获取到的地址0x0000aaadf0f0caca就不是一个地址了。