Tag: ctypes

使用ctypes从Python中获取C中的自定义数据类型的地址

我在C中有一个带有以下字段的vector结构, struct vector { unsigned char* data; unsigned long size; unsigned long elemsize; unsigned long capacity; }; 并且有一些函数相应地对vector实例起作用,例如: struct vector* vector_new(unsigned long elemsize); void vector_delete(struct vector* vec); void vector_push_back(struct vector* vec, void* value, unsigned long elemsize); void vector_reserve(struct vector* vec, unsigned long cap); … 等等(模仿c ++风格的std::vector )。 在我的代码库的其他部分,我有组件结构,例如mirror : struct mirror { double R; double […]

C,Python,ctypes退出代码-1073741819(0xC0000005)

C代码(DLL) #include #include #include struct Doc { wchar_t path[512]; int r; int g; int b; }; struct Docs { struct Doc docs[1000000]; }; struct Color { int r; int g; int b; }; float distance(int a, int b, int c, int d, int e, int f) { return sqrt((ab)*(ab)+(cd)*(cd)+(ef)*(ef)); } struct Doc main(long len, struct […]

基于C-exit代码捕获ctypes中的exception

我正在使用ctypes调用Python/numpy中用C编写的共享库。 这很有效,但是,当在C使用exit函数时, iPython会出现一些意外结果。 请考虑以下示例,其中数组“A”的第一项在C修改。 如果值为负,则应引发exception。 C代码: #include #include #include extern void cfun(double* A) { // raise exception if A[0]<0.0 if ( A[0]<0.0 ) { printf("Negative value of A[0] encountered\n"); exit(1); } // change "A[0]" to it's square A[0] = pow(A[0],2); } 哪个是使用编译的 gcc -c -fPIC fun.c gcc -shared -o test.so fun.o 包装Python -code: import numpy […]

CTypes错误加载调用另一个DLL的DLL

编辑:我在DLL调用Python C / API时发布了部分解决方案来解决这个问题。 这篇文章显示了所涉及的源代码,但似乎源代码不是问题。 我正在使用CTypes来调用在Windows中调用64位C DLL的NASM 64位DLL,但是当我尝试使用CTypes加载它时,我得到“[WinError 126]无法找到指定的模块。” 这两个DLL正确编译并链接在一起,没有编译器或链接器错误,但CTypes无法加载C dll。 我知道这个错误是因为从NASM dll中调用C dll,因为当我消除C调用时,CTypes会加载NASM dll。 研究表明这是一个路径问题。 C dll与NASM dll位于同一文件夹中,它们从该路径链接在一起。 我还将C dll复制到CTypes程序所在的Python文件夹中,但是没有修复它。 当C dll与NASM dll位于同一文件夹中时,为什么会出现路径问题? NASM代码使用NASM编译并与GoLink链接。 C dll在MSYS2中编译并与GCC链接。 这是NASM代码: ; Header Section [BITS 64] [default rel] export Main_Entry_fn extern DllMain section .data align=16 lib_pass_value: dq 0 lib_return_value: dq 0.0 filename: db “Python_Program.py” modulename: db “Test_For_C_API” section […]

Ctypes:将返回的指针映射到结构

我已经尝试阅读关于这个主题的所有问题/答案,但我无法得到任何工作。 我要做的就是将结构发送到共享对象,然后将其返回并可访问。 我已经设法创建一个结构OK,我可以将它传递给共享对象。 我知道这一点,因为如果从结构中返回一个特定的值,它可以正常工作。 Python中的结构定义如下所示: class ROW(ctypes.Structure): _fields_ = [(“Address”,ctypes.c_int16), (“FirstRegister”,ctypes.c_int16), (“NumberOfRegisters”,ctypes.c_int16), (“Sampling”, ctypes.c_int16)] class CONNECTOR(ctypes.Structure): _fields_ = [(“NumberOfRows”, ctypes.c_int16), (“Rows”, ROW * 200)] # Initialise an array of connectors (CON1, CON2, CON11, CON12) ConArray = CONNECTOR * 4 con = ConArray() # Initialise an array of ROW struct RowArray = ROW * 200 row […]

如何通过ctypes对python中包含对它的定义的引用进行建模?

尝试将struct包含在它的定义中,如下所示 foo.c的 typedef struct Foo { struct Foo *foo; } Foo; 例如,如何建模 foo.py class Foo(Structure): _fields_ = [(‘foo’, pointer(Foo))] 当然python没有解释,我可以使用c_void_p而不是指针(Foo),并将其值转换为如下 F = clib.get_foo() cast(F.foo, pointer(Foo)) #although i’m not sure if it would work 但是,有没有办法在python类中对该结构进行建模?

Python Ctypes传入指针并获得结构

这是我在解决实际有用问题之前尝试工作的一个简单示例。 C代码: typedef struct { uint32_t seconds; uint32_t nanoseconds; } geoTime; int myTest(geoTime *myTime){ printf(“Time: %d %d\n”, myTime->seconds, myTime->nanoseconds); myTime->seconds = myTime->nanoseconds; geoTime T = {314, 159}; printf(“MyTime: %d %d retValue: %d %d\n”, myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds); return 314; } Python代码: import ctypes import time import math lib_astro = ctypes.CDLL(“libastroC.so”) class geoTime(ctypes.Structure): _fields_ = [(“seconds”, […]

Python ctypes:用运算符包装c ++类

我想用ctypes包装一个小的测试C ++类,用于python。 该类称为Edge并具有朋友比较(==)运算符。 我在包装python代码中实现比较function有困难。 简洁的Edge标题是: class Edge { private: int p1, p2; public: Edge(const int pp1, const int pp2); ~Edge(){}; friend bool operator==(const Edge &e1, const Edge &e2); }; 我写的python包装器是: from ctypes import * lib = cdll.LoadLibrary(‘./libedge.so’) lib.Edge_new.argtypes = [c_int, c_int] lib.Edge_new.restype = c_void_p lib.compare_edge.argtypes = [c_void_p, c_void_p] lib.compare_edge.restype = c_bool class Edge(object): def __init__(self, […]

ctypes POINTER(c_void_p)在回调函数中

我目前面临着ctypes的问题。 我有一个C函数foo,这样: void** foo(int); 我必须为foo函数定义一个回调函数。 所以: FOO_FUNC = CFUNCTYPE(POINTER(c_void_p), c_int) foo_c = lib.foo foo.argtypes = [c_int] foo.restype = POINTER(c_void_p) 不幸的是,在调用函数时将此回调函数作为参数,它会给出错误: TypeError: invalid result type for callback function 我看不出问题……任何人都可以帮助我吗? 谢谢

将linux的.so文件转换为Windows的.dll文件

我有一些C代码(有各种头文件和一个make文件)在Linux(Ubuntu)中编译为.so文件和一个python程序,它使用Ctypes调用这个共享对象的函数。 现在,我想在Windows上使用这个程序。 在Windows中, Ctypes需要一个.dll文件而不是linux中的.so文件。 那么,有没有办法可以将linux的.so文件转换为.dll文件, Ctypes在Windows中与Ctypes一起使用?