python - 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))
解决方案
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))
推荐阅读
- bigdata - Druid:它如何使用缓存和 OS 页面缓存?
- android - 如何在没有 Identity Toolkit API 的情况下使 Firebase 身份验证完全工作
- sql - 使用 OUTPUT 参数将 Sql INSERT 插入 FILETABLE 会创建超时和死锁
- javascript - 将供应商文件导入 webpack 项目
- scala - 使用 spark 中的数据帧以 writetime 写入 Cassandra
- python-3.x - 使用 numpy 将列转换为特定行
- javascript - 为什么我的计算器功能无法完成数学运算?
- php - 如何使用 php 将 onclick 计数保存到 mysql 表中?
- java - Swagger-UI 泛型映射
- c++ - boost::split 算法崩溃(无法访问内存)