dereferencing the whole Data of c_void_p not only the first byte

谁说我不能喝 提交于 2021-01-27 12:26:17

问题


i have a Question about Pythons ctypes and calling C functions bothering me for a couple days now. I'm working with Python 3.5 and ctypes to wrap a C .dll.

I've got a C Function accepting a void ** as an output parameter. This should contain a Pointer to some 8 Bit RGB Data of a Image after the Call.

foo(void ** bar) 

I declare my Python parameter and function call as following:

>>bar = c_void_p()
>>foo(byref(bar))

And try to recieve the Data:

>>data = c_byte(bar.value)
>>data.value
20

which actually is a reasonable value for the first Pixels 'R'-Byte But i did not manage to reach any Byte beyond this. For my understandig I now get the value of the first Byte the c_void_p points to(?)

I also tried:

>>data = (c_byte*3)(bar.value)

3 is only for test purposes (tried and went up to couple thousands):

>>data
<__main__.c_byte_Array_3 at 0x7faca45de950> #so far so good again?
>>data[0]
20
>>data[1]
0
...
>>data[n]
0

for me it seems like i cant acces any further Bytes again?

when i Try to define bar as a int Pointer (what is recommend by a lot of guys) i get the following:

>>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)

I also worked through anything i found online including: this, this, and a couple more I cant find right now.

Any Help is really appreciated!

edit: whole code of the python file:

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()

call from python to C:

def get_image(handle, imageHandle, imageData, timeout = 0):
 return d.SDK_GetImage(handle, byref(imageHandle), imageData, timeout)

additional information:

  • there actually are more void** output parameters in the code, such as imageHandle. But they do not address any Arrays and seem to work just fine. They are declared as c_void_pand are called byref( ).

  • the RGB Data stored as a Byte stream of Pixels of each Line. so its dimension[0] x dimension[1] x bit-depth long.

  • I can't provide any C Code because it is part of a shared library.


回答1:


This is one way of doing things. Also, ctypes's official doc: [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()

Notes:

  • Since the array allocation place (C or Python) wasn't mentioned, I chose the former option (otherwise, the double pointer (void **) wouldn't make much sense). This adds some complexity to the code (among other things: dealloc - to avoid memory leaks). Using the latter option would "generate" less code, and no need for void** (I'm still wondering why it's needed anyway)
  • void* is a generic type with not very much info. That's why in C, in order to populate the individual bytes, I had to cast it to unsigned char *. Same thing applies to Python. The way I expressed void ** in Python is via POINTER(c_void_p)
  • A lot of the code is for printing purposes. That includes:
    • _get_print_indexes, which is only used to select 5 (DISPLAY_VALUES_COUNT) equidistant (from index perspective) elements in an array
    • _print_array_values - prints the values

Output:

(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:

  • Cleaned the code a bit (some renames, ...)
  • Separated printing (added _print_array_values)
  • Modified the ctypes pointers to be more meaningful. What stands out and can be confusing, is the relationship between a pointer and an array (when wrapped by another (outer) pointer):
    • In C they are somewhat equivalent: both reference the address of the 1st (array) element. Easily cast one to another
    • In Python the array must be wrapped by POINTER (like a pass by value type) in order to be equivalent to the pointer (and cast from / to it)
  • Successfully ran the same code on Lnx


来源:https://stackoverflow.com/questions/51981858/dereferencing-the-whole-data-of-c-void-p-not-only-the-first-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!