我有一个与this问题类似的问题。我基本上是在尝试使用ctypes从Python创建C结构
在C中,我有:
typedef struct Point {
int x;
int y;
} Point ;
Point* makePoint(int x, int y){
Point *point = (Point*) malloc(sizeof (Point));
point->x = x;
point->y = y;
return point;
}
void freePoint(Point* point){
free(point);
}
在Python中,我有:
class Point(ct.Structure):
_fields_ = [
("x", ct.c_int64),
("y", ct.c_int64),
]
lib = ct.CDLL("SRES.dll")
lib.makePoint.restype = ct.c_void_p
pptr = lib.makePoint(4, 5)
print(pptr)
p = Point.from_address(pptr)
print(p)
print(p.x)
print(p.y)
目前,这会输出一组指针:
2365277332448
<__main__.Point object at 0x00000226D2C55340>
21474836484
-8646857406049613808
我如何让这个输出返回我输入的数字,即
2365277332448
<__main__.Point object at 0x00000226D2C55340>
4
5
问题是
c_int64
。 将其更改为c_int32
后,它工作正常。 您可以从C端将c_int64
作为long
此外,您还可以
相关问题 更多 >
编程相关推荐