Tag: ctypes

使用ctypes将python字符串对象转换为c char *

我试图使用ctypes从Python(3.2)向C发送2个字符串。 这是我的Raspberry Pi上项目的一小部分。 为了测试C函数是否正确接收到字符串,我将其中一个放在文本文件中。 Python代码 string1 = “my string 1” string2 = “my string 2” # create byte objects from the strings b_string1 = string1.encode(‘utf-8’) b_string2 = string2.encode(‘utf-8’) # send strings to c function my_c_function(ctypes.create_string_buffer(b_string1), ctypes.create_string_buffer(b_string2)) C代码 void my_c_function(const char* str1, const char* str2) { // Test if string is correct FILE *fp = fopen(“//home//pi//Desktop//out.txt”, […]

包含数组的ctypes结构

我正在尝试使用ctypes 。 我对操纵包含数组的C结构感兴趣。 请考虑以下my_library.c #include typedef struct { double first_array[10]; double second_array[10]; } ArrayStruct; void print_array_struct(ArrayStruct array_struct){ for (int i = 0; i < 10; i++){ printf("%f\n",array_struct.first_array[i]); } } 并假设我已经在共享库中编译它my_so_object.so从Python我可以做这样的事情 import ctypes from ctypes import * myLib = CDLL(“c/bin/my_so_object.so”) class ArrayStruct(ctypes.Structure): _fields_ = [(‘first_array’, ctypes.c_int * 10), (‘second_array’, ctypes.c_int * 10)] def __repr__(self): return ‘ciaone’ […]

使用ctypes模块访问用C编写的DLL时出错

我有一个DLL,只有一个函数,它有五个双精度数和一个整数: __declspec(dllexport) struct res ITERATE(double z_r,double z_i,double c_r, double c_i, int iterations, double limit) 它返回一个自定义的struct caled res,它由一个三双数组组成: struct res { double arr[3]; }; 要返回值,我这样做: struct res result; /*earlier in the code */ result.arr[0] = z_real; /*Just three random doubles*/ result.arr[1] = z_imag; result.arr[2] = value; return result; 我用MinGW编译它,我试图在python中使用它来做这样的事情: form ctypes import * z = [0.0,0.0] […]

通过Ctypes从C到Python – 将函数指针的结构包装到静态函数

我有一个像这样的C库中的结构。 DataFn中的函数指针指向静态函数。 。H struct Data { int i; int *array; }; typedef struct { bool (* const fn1) (struct Data*, const char *source); …. } DataFn; extern DataFn const DATAFUNC 使用objdump,该表仅包含DATAFUNC和gcc中的一些其他内容。 这在C中很好,其中调用fn1就像DATAFUNC.fn1(…,…),但是这样的东西怎么会被包裹起来所以fn1可以用python w / ctypes调用? 示例python libc = ctypes.cdll.LoadLibrary(“./data.so”) print(libc.DATAFUNC) 导致 这是类似的,但没有工厂function。

Python ctypes和函数调用

我的朋友制作了一个适用于x86的小型概念validation汇编程序。 我决定将它移植到x86_64,但我立即遇到了问题。 我在C中编写了一小段程序,然后编译并objdumped代码。 之后我将它插入到我的python脚本中,因此x86_64代码是正确的: from ctypes import cast, CFUNCTYPE, c_char_p, c_long buffer = ”.join(map(chr, [ #0000000000000000 : 0x55, # push %rbp 0x48, 0x89, 0xe5, # mov %rsp,%rbp 0x48, 0x89, 0x7d, 0xf8, # mov %rdi,-0x8(%rbp) 0x48, 0x8b, 0x45, 0xf8, # mov -0x8(%rbp),%rax 0x48, 0x83, 0xc0, 0x0a, # add $0xa,%rax 0xc9, # leaveq 0xc3, # retq ])) […]

使用ctypes将2d numpy数组传递给c

使用ctypes将numpy 2d – 数组传递给ac函数的正确方法是什么? 到目前为止我的当前方法(导致段错误): c代码: void test(double **in_array, int N){ int i,j; for(i = 0; i<N; i++){ for(j = 0; j<N; j++){ printf("%e \t", in_array[i][j]); } printf("\n"); } } python代码: from ctypes import * import numpy.ctypeslib as npct array_2d_double = npct.ndpointer(dtype=np.double,ndim=2, flags=’CONTIGUOUS’) liblr = npct.load_library(‘libtest.so’, ‘./src’) liblr.test.restype = None liblr.test.argtypes = [array_2d_double, c_int] x […]

为什么这个ctypes代码不能与Python 3.3一起使用,但是可以与Python 2.7一起使用?

所以我正在尝试使用ctypes模块创建一个Python 3.3程序来更改Windows桌面背景。 我在Python 2.7中测试了以下代码,它运行得很好。 但它只适用于Python 3.3! 我正在使用Windows 7.这是代码: import ctypes SPI_SETDESKTOPWALLPAPER=20 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER, 0,”C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg”, 3)