我现在有一个关于Python ctypes和调用C函数的问题,困扰着我几天。我正在使用Python 3.5和ctypes包装C .dll。
我有一个C函数,它接受void **
作为输出参数。调用后,它应该包含一个指向图像的某些8位RGB数据的指针。
foo(void ** bar)
我声明我的Python参数和函数调用如下:
>>bar = c_void_p()
>>foo(byref(bar))
并尝试接收数据:
>>data = c_byte(bar.value)
>>data.value
20
对于第一个像素“ R”字节实际上是一个合理的值 但是我没有设法超出此范围。为了我的理解,我现在得到c_void_p指向(?)
的第一个字节的值我也尝试过:
>>data = (c_byte*3)(bar.value)
3仅用于测试目的(已尝试并达到数千个):
>>data
<__main__.c_byte_Array_3 at 0x7faca45de950> #so far so good again?
>>data[0]
20
>>data[1]
0
...
>>data[n]
0
对我来说,好像我无法再访问其他字节了?
当我尝试将bar
定义为int指针时(很多人推荐使用),我得到以下信息:
>>bar = POINTER(c_int)()
>>foo(byref(bar))
>>bar
<__main__.LP_c_int at 0x7fcf71d9d8c8> #so far so good?
>>bar.contents
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
我还研究了在网上找到的所有内容,包括: this,this,还有我目前找不到的其他几个。
我们非常感谢您的帮助!
修改: python文件的完整代码:
from ctypes import *
from ctypes_wrapper_dijsdk import *
init()
guids = find_cameras()
handle = c_void_p()
open_camera(guids, handle, 0)
start_acquisition(handle, 1)
imageHandle = c_void_p()
VOIDPP = POINTER(c_void_p)
imageData = VOIDPP(c_void_p())
get_image(handle, imageHandle, imageData)
byte_buf = cast(imageData.contents, POINTER(c_ubyte * (1920)))
for x in range(0, (1920)-1 ):
print("value: {:3d}".format(byte_buf.contents[x]))
dimension = (c_int*2)()
get_int_parameter(imageHandle, 0x20000103, dimension, 2)
value = c_void_p()
get_int_parameter(imageHandle, 0x20000200, value)
#from now on would be able to get the ImageData Size
print("ImageFormat = ", value.value, "see SDK.h")
release_image(imageHandle)
close_camera(handle)
exit()
从python调用到C:
def get_image(handle, imageHandle, imageData, timeout = 0):
return d.SDK_GetImage(handle, byref(imageHandle), imageData, timeout)
其他信息:
在代码中实际上有更多的void**
输出参数,例如imageHandle
。但是它们没有解决任何数组问题,并且看起来还不错。它们被声明为c_void_p
,被称为byref( )
。
以每行像素的字节流存储的RGB数据。因此其尺寸为[0] x尺寸为[1] x位深度长。
我无法提供任何C代码,因为它是共享库的一部分。
答案 0 :(得分:2)
这是一种处理方式。另外, ctypes 的官方文档为[Python 3.5]: ctypes - A foreign function library for Python。
dll.c :
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
#define C_TAG "From C"
#define PRINT_MSG_0() printf("%s - [%s] (%d) - [%s]\n", C_TAG, __FILE__, __LINE__, __FUNCTION__)
#define PRINT_ERR_1S(ARG0) printf("%s: %s\n", C_TAG, ARG0)
DLL_EXPORT int test(void **pptr, size_t count) {
PRINT_MSG_0();
if (!pptr) {
PRINT_ERR_1S("NULL pointer received");
return -1;
}
if (*pptr) {
PRINT_ERR_1S("Non NULL inner pointer received");
return -2;
}
unsigned char *buf = (unsigned char*)malloc(count);
for (size_t i = 0; i < count; i++) {
buf[i] = (i + 1) % 0x100;
}
*pptr = buf;
return 0;
}
DLL_EXPORT void dealloc(void **pptr) {
PRINT_MSG_0();
if ((pptr) && (*pptr)) {
free(*pptr);
*pptr = NULL;
}
}
code.py :
import sys
import math
from ctypes import c_ubyte, c_int, c_size_t, c_void_p, \
POINTER, CDLL, \
cast
VoidPtrPtr = POINTER(c_void_p)
dll_dll = CDLL("./dll.dll")
test_func = dll_dll.test
test_func.argtypes = [VoidPtrPtr, c_size_t]
test_func.restype = c_int
dealloc_func = dll_dll.dealloc
dealloc_func.argtypes = [c_void_p]
DISPLAY_VALUES_COUNT = 5
FORMAT_STRING_PAT = " idx: {{:{:d}d}} - value: {{:3d}}"
def _get_print_indexes(array_size, values_count):
if array_size <= 0 or values_count <= 1 or values_count > array_size:
raise ValueError("Invalid args")
yield 0
if array_size > 1:
if values_count > 2:
interval_size = array_size / (values_count - 1)
for idx in range(1, values_count - 1):
yield int(round(idx * interval_size))
yield array_size - 1
def _print_array_values(array, array_size, values_count=DISPLAY_VALUES_COUNT):
index_width = math.ceil(math.log10(array_size))
format_string = FORMAT_STRING_PAT.format(index_width)
for idx in _get_print_indexes(array_size, values_count):
print(format_string.format(idx, array.contents.contents[idx]))
def main():
sizes = [
10,
100,
500,
1920 * 1080 * 3,
]
for size in sizes:
UByteArr = c_ubyte * size
UByteArrPtr = POINTER(UByteArr)
UByteArrPtrPtr = POINTER(UByteArrPtr)
print("\nSize: {:d}".format(size))
data = UByteArrPtrPtr(UByteArrPtr())
print("data: {:}, data.contents: {:}".format(data, data.contents))
#print(addressof(data), addressof(data.contents))
ptr = cast(data, VoidPtrPtr)
res = test_func(ptr, size)
if res < 0:
print("{:s} returned {:d}. Moving on...\n".format(test_func.__name__, res))
continue
print("data: {:}, data.contents: {:}".format(data, data.contents))
_print_array_values(data, size)
dealloc_func(data)
print("data: {:}, data.contents: {:}".format(data, data.contents))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
注释:
void **
)没什么意义。这给代码增加了一些复杂性(除其他外:dealloc
-避免内存泄漏)。使用后一个选项将“生成”更少的代码,并且不需要void**
(我仍然想知道为什么仍然需要它)void*
是一个通用类型,没有太多信息。这就是为什么在 C 中,为了填充各个字节,我不得不 cast 到unsigned char *
。同样的情况适用于 Python 。我在 Python 中表达void **
的方式是通过POINTER(c_void_p)
_get_print_indexes
,仅用于选择 5 (DISPLAY_VALUES_COUNT
)等距(从索引角度看)数组中的元素_print_array_values
-打印值输出:
(py35x64_test) e:\Work\Dev\StackOverflow\q051981858>"c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64 (py35x64_test) e:\Work\Dev\StackOverflow\q051981858>dir /b code.py dll.c (py35x64_test) e:\Work\Dev\StackOverflow\q051981858>cl /nologo dll.c /DDLL /link /DLL /OUT:dll.dll dll.c Creating library dll.lib and object dll.exp (py35x64_test) e:\Work\Dev\StackOverflow\q051981858>dir /b code.py dll.c dll.dll dll.exp dll.lib dll.obj (py35x64_test) e:\Work\Dev\StackOverflow\q051981858>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 Size: 10 data: <__main__.LP_LP_c_ubyte_Array_10 object at 0x000001F8189FBBC8>, data.contents: <__main__.LP_c_ubyte_Array_10 object at 0x000001F8189FBC48> From C - [dll.c] (16) - [test] data: <__main__.LP_LP_c_ubyte_Array_10 object at 0x000001F8189FBBC8>, data.contents: <__main__.LP_c_ubyte_Array_10 object at 0x000001F8189FBCC8> idx: 0 - value: 1 idx: 2 - value: 3 idx: 5 - value: 6 idx: 8 - value: 9 idx: 9 - value: 10 From C - [dll.c] (35) - [dealloc] data: <__main__.LP_LP_c_ubyte_Array_10 object at 0x000001F8189FBBC8>, data.contents: <__main__.LP_c_ubyte_Array_10 object at 0x000001F8189FBD48> Size: 100 data: <__main__.LP_LP_c_ubyte_Array_100 object at 0x000001F8189FBCC8>, data.contents: <__main__.LP_c_ubyte_Array_100 object at 0x000001F8189FBDC8> From C - [dll.c] (16) - [test] data: <__main__.LP_LP_c_ubyte_Array_100 object at 0x000001F8189FBCC8>, data.contents: <__main__.LP_c_ubyte_Array_100 object at 0x000001F8189FBC48> idx: 0 - value: 1 idx: 25 - value: 26 idx: 50 - value: 51 idx: 75 - value: 76 idx: 99 - value: 100 From C - [dll.c] (35) - [dealloc] data: <__main__.LP_LP_c_ubyte_Array_100 object at 0x000001F8189FBCC8>, data.contents: <__main__.LP_c_ubyte_Array_100 object at 0x000001F8189FBE48> Size: 500 data: <__main__.LP_LP_c_ubyte_Array_500 object at 0x000001F8189FBC48>, data.contents: <__main__.LP_c_ubyte_Array_500 object at 0x000001F8189FBEC8> From C - [dll.c] (16) - [test] data: <__main__.LP_LP_c_ubyte_Array_500 object at 0x000001F8189FBC48>, data.contents: <__main__.LP_c_ubyte_Array_500 object at 0x000001F8189FBDC8> idx: 0 - value: 1 idx: 125 - value: 126 idx: 250 - value: 251 idx: 375 - value: 120 idx: 499 - value: 244 From C - [dll.c] (35) - [dealloc] data: <__main__.LP_LP_c_ubyte_Array_500 object at 0x000001F8189FBC48>, data.contents: <__main__.LP_c_ubyte_Array_500 object at 0x000001F8189FBF48> Size: 6220800 data: <__main__.LP_LP_c_ubyte_Array_6220800 object at 0x000001F8189FBDC8>, data.contents: <__main__.LP_c_ubyte_Array_6220800 object at 0x000001F818A62048> From C - [dll.c] (16) - [test] data: <__main__.LP_LP_c_ubyte_Array_6220800 object at 0x000001F8189FBDC8>, data.contents: <__main__.LP_c_ubyte_Array_6220800 object at 0x000001F8189FBEC8> idx: 0 - value: 1 idx: 1555200 - value: 1 idx: 3110400 - value: 1 idx: 4665600 - value: 1 idx: 6220799 - value: 0 From C - [dll.c] (35) - [dealloc] data: <__main__.LP_LP_c_ubyte_Array_6220800 object at 0x000001F8189FBDC8>, data.contents: <__main__.LP_c_ubyte_Array_6220800 object at 0x000001F8189FBEC8>
@ EDIT0 :
_print_array_values
)POINTER
包裹(如按值传递类型),以便与等效指向指针(并从/投射到指针)