首页 > 解决方案 > Python - ctypes - 访问缓冲区返回作为 dll 库中的指针 - 数据翻译 SDK

问题描述

我正在使用 Datatranlation 的 DataAcq SDK 开发一个项目。

我正在尝试访问缓冲区并设置它的值。

在 C 中如何使用它的示例:

/* allocate the output buffer, sets handler for buffer */
CHECKERROR (olDmCallocBuffer(0,0,(ULNG) size,2,&hbuf));
/* Get pointer to buffer */
CHECKERROR (olDmGetBufferPtr(board.hbuf,(LPVOID*)&lpbuf));

/* fill the output buffer*/

lpbuf[i] = (UINT) some value

/* Put the buffer to the DAC */

CHECKERROR (olDaPutBuffer(board.hdass, hbuf));

我正在尝试使用 ctypes 在 Python 中做同样的事情:

_buffer = ctypes.c_void_p()

    # Create buffer:
    check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
                                        ctypes.c_uint(0x0000),
                                        ctypes.c_uint(2),
                                        ctypes.c_uint(4),
                                        ctypes.byref(_buffer)))
    # Get pointer to buffer:
    buf_ptr = ctypes.c_void_p()
    check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))

但是我怎样才能像上面 C 中的示例那样访问和设置元素呢?我可以在 Python 中进行引用,我可以修改但如何将其解析回 olDaPutBuffer 函数?

编辑:

memmove用from解决了它ctypes

_buffer = ctypes.c_void_p()

# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
                                    ctypes.c_uint(0x0000),
                                    ctypes.c_uint(2),
                                    ctypes.c_uint(4),
                                    ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))

c_type_buf = ctypes.c_buffer(some values)
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))

标签: pythonpointersdllctypes

解决方案


memmove用from解决了它ctypes

_buffer = ctypes.c_void_p()

# Create buffer:
check_error(memlib.olDmCallocBuffer(ctypes.c_uint(0x0000),
                                    ctypes.c_uint(0x0000),
                                    ctypes.c_uint(2),
                                    ctypes.c_uint(4),
                                    ctypes.byref(_buffer)))
# Get pointer to buffer:
buf_ptr = ctypes.c_void_p()
check_error(memlib.olDmGetBufferPtr(_buffer, ctypes.byref(buf_ptr)))

# Create new buffer
c_type_buf = ctypes.c_buffer(some values)

# Copy new buffer to address of the original buffer
ctypes.memmove(buf_ptr, ctypes.byref(c_type_buf), len(c_type_buf))

推荐阅读