【python使用ctypes调用扩展模块的实例方法】教程文章相关的互联网学习教程文章

python – 在ctypes.Structure中使用枚举【代码】

我有一个我通过ctypes访问的结构:struct attrl {char *name;char *resource;char *value;struct attrl *next;enum batch_op op; };到目前为止,我有Python代码,如:# struct attropl class attropl(Structure):pass attrl._fields_ = [("next", POINTER(attropl)),("name", c_char_p),("resource", c_char_p),("value", c_char_p),但我不确定如何使用batch_op枚举.我应该将它映射到c_int还是?解决方法:至少对于GCC而言,枚举...

Python ctypes cdll.LoadLibrary,实例化一个对象,执行其方法,私有变量地址被截断【代码】

我在c中编写了一个dll库,使用vs2017 64位编译,并尝试使用64位python3.6加载它.但是,对象的成员变量的地址被截断为32位. 这是我的sim.c文件,它被编译为sim.dll:class Detector { public:Detector();void process(int* pin, int* pout, int n);private:int member_var; };Detector::Detector() {memset(&member_var, 0, sizeof(member_var));myfile.open("addr_debug.txt");myfile << "member_var init address: " << &member_var <...

在将Python 2代码移植到Python 3时处理ctypes和ASCII字符串【代码】

昨晚我厌倦了并开始将PyVISA移植到Python 3(进展到这里:https://github.com/thevorpalblade/pyvisa). 只要我将设备地址(嗯,任何字符串真的)作为ASCII字符串而不是默认的unicode字符串传递(例如,HP = vida.instrument(b“GPIB :: 16”)有效,而HP = vida.instrument(“GPIB :: 16”)没有,引发了ValueError. 理想情况下,最终用户不必关心字符串编码.关于我应该如何处理的任何建议? ctypes类型定义中的某些东西也许? 就目前而言,相关...

python – 无法从ctypes获取fortran函数输出【代码】

我正在尝试使用ctypes从Python调用Fortran函数.我试图从子程序和函数(两者都具有相同的功能)获得结果,但我无法从函数中获得预期的输出,而子程序运行良好.问题是我有很多库使用Fortran函数而不是子例程.Fortran函数和ctypes有什么问题吗? 一段Fortran代码:MODULE Vector ! Public typesTYPE VectorTypePRIVATEDOUBLE PRECISION, DIMENSION(3):: components = 0.0d0END TYPE VectorType !----------------------------------------...

尽快将ctypes数据转换为python字符串【代码】

我正在尝试在PyQt4中编写一个视频应用程序,我使用Python ctypes挂钩到一个旧的传统视频解码器库.该库为我提供了32位ARGB数据,我需要将其转换为QImage.我的工作方式如下:# Copy the rgb image data from the pointer into the buffer memmove(self.rgb_buffer, self.rgb_buffer_ptr, self.buffer_size)# Copy the buffer to a python string imgdata = "" for a in self.rgb_buffer:imgdata = imgdata + a# Create a QImage from t...

Python ctypes地址的CFuncType【代码】

与我的other question相关 如何获取CFuncType对象的地址(acctual函数指针)? addressof()不报告正确的地址. C代码:extern "C" _declspec(dllexport) int addr(int (*func)()) {int r = (int)func;return r; }Python代码:def test():return 42t = CFUNCTYPE(c_int) f = t(test)print addressof(f) print dll.addr(f)输出:7030864 3411932试图从C调用*(7030864)导致崩溃,但调用*(3411932)按预期工作. addressof()有什么问题?解决...

Python和ctypes:为什么printf(“%f”,10.1)是错误的【代码】

我对python中的ctypes有疑问from ctypes import * printf = cdll.msvcrt.printf printf("%s\n", "Hello World!") printf("%d\n", 100) printf("%f\n", 10.1)结果:Hello World! 100 Traceback (most recent call last):File "C:\Users\Windows7\Desktop\test.py", line 5, in <module>printf("%f\n", 10.1) ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: Don't know how to convert parameter 2我知道如何纠正...

使用Python ctypes访问c-structs数组【代码】

我有一个C函数,它在传递给的地址处分配内存,并通过Python访问.指针内容确实包含C代码中的结构数组,但是我无法让ctypes在第0个元素之外正确访问数组.如何获得正确的内存偏移量才能访问非零元素?如果我尝试使用他们的ctypes.memset函数,Python的ctypes.memset会抱怨TypeErrors.typedef struct td_Group {unsigned int group_id;char groupname[256];char date_created[32];char date_modified[32];unsigned int user_modified;unsig...

c – 使用带有python的DLL(使用ctypes),不工作【代码】

我正在尝试编写一个可以在Python(2.7)中导入的DLL,并且我遇到了“使其工作”的困难.当我使用WinDLL()或windll.LoadLibrary()在Python中加载库时,测试导出的函数,我得到的输出是空的.如果我向TestFunction()添加一个参数,它会引发一个ValueError,它声明可能有很多参数(实际上并非如此). Python文件:from ctypes import *x = windll.LoadLibrary('./pymod.dll') print x.TestFunction(123) #Should return 123.main.h:#ifndef __MA...

使用Ctypes从Python传递C结构【代码】

我试图在一个使用结构作为参数的C库中调用一个函数.我该怎么做呢?我已经创建了类似于文档中所做的结构的Ctypes表示,但我直接传递它不起作用. 我试图找到例子,但我发现的所有例子都是通过指向结构的指针.解决方法:没有看到你的代码就很难说,但我的猜测是你必须实例化结构.来自文档:from ctypes import *class POINT(Structure):_fields_ = [("x", c_int),("y", c_int)]point = POINT(10, 20)最后一行创建了一个名为point的POINT结...

无法使用ctypes将参数传递给dll(Python)【代码】

使用ctypes时遇到一些问题 我有一个带有以下界面的testdllextern "C" {// Returns a + bdouble Add(double a, double b);// Returns a - bdouble Subtract(double a, double b);// Returns a * bdouble Multiply(double a, double b);// Returns a / bdouble Divide(double a, double b); }我也有一个.def文件,所以我有“真正的”名字LIBRARY "MathFuncsDll" EXPORTSAddSubtractMultiplyDivide我可以通过ctype从dll加载和访问该函数...

python:将bytearray转换为ctypes Struct【代码】

我有一个’n’字节bytearray.这与定义的ctypes.Sturcture之一相同.我想将这个bytearray类型化为这个结构.这样我就可以访问这个成员了.我怎样才能做到这一点?class ABC(Structure):_fields_ = [("a", c_uint), ("b", c_ushort), ("c", c_ushort)]class DEF(Structure):_fields_ = [("abc", ABC), ("i", I)]b = bytearray(b'\x88\x08\xc0\xf9\x02\x85\x10\x00\xcc')s = DEF() print(s.abc.a)如何获得上述print语句的正确值?解决方法...

使用ctypes进行回调(如何从C调用python函数)【代码】

是否可以从C dll函数调用Python函数? 我们考虑这个C函数:void foo( void (*functionPtr)(int,int) , int a, int b);在Python上,我想调用foo并将回调设置为Python函数:def callback(a, b):print("foo has finished its job (%d, %d)" % (a.value,b.value))dll.foo( callback, c_int(a), c_int(b) )不幸的是,ctypes文档对这个主题非常清楚,上面的代码不起作用.解决方法: import ctypes as c@c.CFUNCTYPE(None, c.c_int, c.c_int) ...

python – ctypes:将字符串转换为函数?【代码】

我正在阅读文章Tips for Evading Anti-Virus During Pen Testing,并对给定的Python程序感到惊讶:from ctypes import * shellcode = '\xfc\xe8\x89\x00\x00....'memorywithshell = create_string_buffer(shellcode, len(shellcode)) shell = cast(memorywithshell, CFUNCTYPE(c_void_p)) shell()shellcode缩短了.有人可以解释发生了什么吗?我熟悉Python和C,我已经尝试过读取ctypes模块,但还有两个主要问题: >什么存储在shellcode...

python – CTYPES:过程可能调用太多参数(超过92个字节)【代码】

我有一个dll__declspec(dllexport) int __cdecl wrp_ANN_sel_HDGT(int steps,double in_TNH[], double in_TNL[], double in_pamb[], double in_Tamb[], double in_CPD[],double in_T3[], double in_Tex[], double in_IGV[], double in_Dpin[], double in_Dpout[],double in_LHV[], double in_RH[], short in_model, double in_pamb_ref, double in_Dpin_ref,double in_Dpout_ref, double in_LHV_ref, double in_RH_ref, double out...

实例 - 相关标签