简体   繁体   English

python中的ctypes,在DLL中调用函数时出现问题

[英]ctypes in python, problem calling a function in a DLL

Hey! 嘿! as you might have noticed I have an annoying issue with ctypes. 您可能已经注意到,我对ctypes有一个烦人的问题。 I'm trying to communicate with an instrument and to do so I have to use ctypes to communicate with the DLL driver. 我正在尝试与一种仪器进行通信,为此,我必须使用ctypes与DLL驱动程序进行通信。

so far I've managed to export the DLL by doing this: 到目前为止,我已经通过执行以下操作导出了DLL:

>>> from ctypes import *
>>>maury = WinDLL( 'MLibTuners')
>>> maury
(WinDLL 'MlibTuners', handle 10000000 at 9ef9d0)
>>> maury.get_tuner_driver_version()
(_FuncPtr object at 0x009F6738)
>>> version_string = create_string_buffer(80)
>>> maury.get_tuner_driver_version(version_string)
2258920
>>> print version_string.value
'Maury Microwave MT993V04 Tuner Driver DLL, Version 1.60.00, 07/25/2007'

And it works pretty well, according to the documentation it is supposed to save the Tuner Driver DLL in the 80 byte string given as a parameter. 而且效果很好,根据文档,它应该将Tuner Driver DLL保存在作为参数给出的80字节字符串中。 However when I try to use the function called add_tuner it fails. 但是,当我尝试使用名为add_tuner的函数时,它将失败。 This is what the documentation says: 这就是文档所说的:

short add_tuner(short tuner_number, char model[], short serial_number,
               short ctlr_num, short ctlr_port, short *no_of_motors, long max_range[],
               double *fmin, double *fmax, double *fcrossover, char error_string[])

this is how I tried to call the function above: The parameters that are changed are all the pointers and max_range[], according to the manual the values below are correct too, i just don't know why I keep getting a windows access violation writing 0x00000000 这就是我尝试调用上面的函数的方式:更改的参数是所有指针和max_range [],根据手册,下面的值也正确,我只是不知道为什么我不断收到Windows访问冲突写0x00000000

no_motors = pointer(c_short())
f_min = pointer(c_double())
f_max = pointer(c_double())
f_crossover = pointer(c_double())
maury.add_tuner(c_short(0), c_char_p('MT982EU'), c_short(serial_number), c_short(0),
                                c_short(1),no_motors, c_long(),
f_min,f_max,f_crossover, create_string_buffer(80))

The serial number is given however censored by refering to a variable. 但是,序列号是通过引用变量来检查的。 Someone know what to do?, do you see any errors with my input? 有人知道该怎么办吗?您看到我的输入有任何错误吗?

Thanks /Mazdak 谢谢/ Mazdak

I figure it's the value you pass at the long max_range[] argument. 我认为这是您在long max_range[]参数中传递的值。 The function expects a pointer to a long integer there (it asks for an array of long integers), but you're passing a long value of zero (result of the c_long() call), which is implicitly cast to a null pointer. 该功能需要一个指向一个long整型那里(它要求阵列long整数),但你传递的零(的结果很 c_long()这是隐式转换为空指针调用)。 I suspect the function then tries to write to the address passed at max_range , ie. 我怀疑函数然后尝试写入在max_range传递的地址,即。 the null pointer, hence the access violation at address 0x00000000 . 空指针,因此在地址0x00000000的访问冲突。

To create an array of long s to pass in max_range , you first create the array type by multiplying the array data type with the size of the array (somewhat verbose for clarity): 要创建一个long数组以传递给max_range ,您首先要通过将数组数据类型与数组大小相乘(为了清楚起见,有些冗长)来创建数组类型:

array_size = 3
ThreeLongsArrayType = c_long * array_size

You can then instantiate an array like you would with any other Python class: 然后,您可以像使用任何其他Python类一样实例化数组:

array = ThreeLongsArrayType()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM