python - 如何将 bytearray 的一部分复制到 c_void_p 引用的内存,反之亦然?

标签 python ctypes

在 Python 应用程序中,我有一个包含一些数据的字节数组。我的python代码由一些外部 native 库(DLL/so/dylib)调用(使用ctypes及其 callback functions )。回调函数之一包括指向在该外部库中分配的非托管缓冲区的指针。

我需要将字节数组的一部分复制到此非托管缓冲区中,或将非托管缓冲区的内容复制到特定位置的字节数组中。

简单来说,我有

def copy_from_python_callback(c_void_p_parameter : c_void_p, offset : int, size : int):
  managed_buf = bytearray(some_size) # suppose we have data in this buffer already
  unmanaged_buf = c_void_p_parameter
  # what do I need to do here?
  # src_buf = pointer_to_specific_byte_in_managed_buf
  memmove(unmanaged_buf, src_buf, size)

def copy_to_python_callback(c_void_p_parameter : c_void_p, offset : int, size : int):
  managed_buf = bytearray(some_size) #some_size is assumed to be larger than offset + size
  unmanaged_buf = c_void_p_parameter
  # what do I need to do here?
  # dst_buf = pointer_to_specific_byte_in_managed_buf
  memmove(dst_buf, unmanaged_buf, size)

在其他语言中,答案很简单 - 我要么调用专用方法(例如在 .NET Framework 的 Marshal 类中),要么获取指向字节数组中特定字节的指针(在 C++ 或 Pascal 等 native 语言中) )并完成。不幸的是,我不知道如何在没有中间 bytes() 或类似缓冲区的情况下在 Python 中执行这些操作。

我有一些使用中间 bytes() 实例的方法,但是仅仅因为无法获取指针而复制数据对我来说似乎很奇怪。

如果可能的话,我正在寻找与版本无关的解决方案,但也可以使用仅包含 python3 的解决方案。先感谢您。

最佳答案

列出 [Python.Docs]: ctypes - A foreign function library for Python

您正在寻找的东西是可能的。数组(CType)派上用场。
下面是一个“小”示例。出于演示目的,缓冲区仅包含“人类友好的”char

dll00.c:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#if defined(_WIN32)
#  define DLL00_EXPORT_API __declspec(dllexport)
#else
#  define DLL00_EXPORT_API
#endif

#define C_TAG "From C - "


typedef int (*ReadFunc)(void *ptr, uint32_t offset, uint32_t size);
typedef int (*WriteFunc)(void *ptr, uint32_t offset, uint32_t size);

#if defined(__cplusplus)
extern "C" {
#endif

DLL00_EXPORT_API int testBufferCallbacks(uint32_t bufSize,
                                         ReadFunc read, uint32_t readOffset, uint32_t readSize,
                                         WriteFunc write, uint32_t writeOffset, uint32_t writeSize);

#if defined(__cplusplus)
}
#endif


void prinBuffer(const uint8_t *buf, uint32_t size) {
    printf("%sBuffer (size %d) 0x%016llX\n  Contents: ", C_TAG, size, (uint64_t)buf);
    for (uint32_t i = 0; i < size; i++) {
        printf("%c", ((uint8_t*)buf)[i]);
    }
    printf("\n");
}

int testBufferCallbacks(uint32_t bufSize,
                        ReadFunc read, uint32_t readOffset, uint32_t readSize,
                        WriteFunc write, uint32_t writeOffset, uint32_t writeSize) {
    const uint8_t charOffset = 0x41;
    void *buf = malloc(bufSize);
    uint8_t *cBuf = (uint8_t*)buf;
    for (uint32_t i = 0; i < bufSize; i++) {
        cBuf[i] = (uint8_t)((i + charOffset) % 0x100);
    }
    prinBuffer(cBuf, bufSize);
    if ((read != NULL) && (readSize > 0)) {
        printf("\n%sCalling read(0x%016llX, %u, %u)...\n", C_TAG, (uint64_t)buf, readOffset, readSize);
        read(buf, readOffset, readSize);
    }
    if ((write != NULL) && (writeSize > 0)) {
        printf("\n%sCalling write(0x%016llX, %u, %u)...\n", C_TAG, (uint64_t)buf, writeOffset, writeSize);
        write(buf, writeOffset, writeSize);
        prinBuffer(cBuf, bufSize);
    }
    if ((read != NULL) && (readSize > 0)) {
        printf("\n%sCalling read(0x%016llX, %u, %u)...\n", C_TAG, (uint64_t)buf, readOffset, readSize);
        read(buf, readOffset, readSize);
    }
    prinBuffer(cBuf, bufSize);
    free(buf);
    printf("\n%sDone.\n", C_TAG);
    return 0;
}

code00.py:

#!/usr/bin/env python3

import sys
import ctypes as ct


DLL_NAME = "./dll00.dll"

ReadFunc = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_uint32, ct.c_uint32)
WriteFunc = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_uint32, ct.c_uint32)


def create_bytearray(size, offset_char=0x61):
    contents = "".join(chr(i) for i in range(offset_char, offset_char + size))
    return bytearray(contents.encode())


def read_c_buf(buf : ct.c_void_p, offset : ct.c_uint32, size : ct.c_uint32):
    print("C buf: 0x{0:016X}".format(buf))
    ba = create_bytearray(0x1A)
    print("Python initial buffer: {0:}".format(ba))
    UCharArr = ct.c_uint8 * size
    uchar_arr = UCharArr.from_buffer(ba, offset)  # Shared memory
    ct.memmove(uchar_arr, buf, size)
    print("Python final buffer: {0:}\n".format(ba))
    return 0


def write_c_buf(buf : ct.c_void_p, offset : ct.c_uint32, size : ct.c_uint32):
    print("C buf: 0x{0:016X}".format(buf))
    ba = create_bytearray(size + offset, offset_char=0x30 - offset)
    print("Python buffer: {0:}\n".format(ba))
    UCharArr = ct.c_uint8 * size
    uchar_arr = UCharArr.from_buffer(ba, offset)  # Shared memory
    ct.memmove(buf, uchar_arr, size)
    return 0


def main(*argv):
    dll00 = ct.CDLL(DLL_NAME)
    testBufferCallbacks = dll00.testBufferCallbacks
    testBufferCallbacks.argtypes = (ct.c_uint32, ReadFunc, ct.c_uint32, ct.c_uint32, WriteFunc, ct.c_uint32, ct.c_uint32)
    testBufferCallbacks.restype = ct.c_int

    read_callback = ReadFunc(read_c_buf)
    buf_size = 0x1A
    read_offset = 10
    read_size = 16
    write_callback = WriteFunc(write_c_buf)
    write_offset = 5
    write_size = 10
    res = testBufferCallbacks(buf_size, read_callback, read_offset, read_size, write_callback, write_offset, write_size)
    print("\n{0:s} returned: {1:d}".format(testBufferCallbacks.__name__, res))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q059255471]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.17
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

[prompt]> dir /b
code00.py
code01.py
dll00.c

[prompt]> cl /nologo /MD /DDLL dll00.c  /link /NOLOGO /DLL /OUT:dll00.dll
dll00.c
   Creating library dll00.lib and object dll00.exp

[prompt]> dir /b
code00.py
code01.py
dll00.c
dll00.dll
dll00.exp
dll00.lib
dll00.obj

[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 064bit on win32

From C - Buffer (size 26) 0x0000016BFE546EB0
  Contents: ABCDEFGHIJKLMNOPQRSTUVWXYZ

From C - Calling read(0x0000016BFE546EB0, 10, 16)...
C buf: 0x0000016BFE546EB0
Python initial buffer: bytearray(b'abcdefghijklmnopqrstuvwxyz')
Python final buffer: bytearray(b'abcdefghijABCDEFGHIJKLMNOP')


From C - Calling write(0x0000016BFE546EB0, 5, 10)...
C buf: 0x0000016BFE546EB0
Python buffer: bytearray(b'+,-./0123456789')

From C - Buffer (size 26) 0x0000016BFE546EB0
  Contents: 0123456789KLMNOPQRSTUVWXYZ

From C - Calling read(0x0000016BFE546EB0, 10, 16)...
C buf: 0x0000016BFE546EB0
Python initial buffer: bytearray(b'abcdefghijklmnopqrstuvwxyz')
Python final buffer: bytearray(b'abcdefghij0123456789KLMNOP')

From C - Buffer (size 26) 0x0000016BFE546EB0
  Contents: 0123456789KLMNOPQRSTUVWXYZ

From C - Done.

testBufferCallbacks returned: 0

Done.

不用说,超出(C)缓冲区的边界,会产生U未定义的B行为(并且我的代码不执行此类检查)。

关于python - 如何将 bytearray 的一部分复制到 c_void_p 引用的内存,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255471/

相关文章:

Python:垃圾收集器行为与 ctypes

python - sqlalchemy : Optimizing a query using joins

Python 单元测试 : how do I test the argument in an Exceptions?

python - 在 Tensorflow 中,为什么只有在准备导出模型时才为其添加激活函数?

python变量范围

python - 如何比较两个 ctypes 对象是否相等?

python - 使用 ctypes 从共享库映射全局变量

python - 具有大量零值作为缺失值的数据集。我应该怎么办?

python - 在Python中为系统用户更改壁纸

python - 返回与指针赋值之间的 c_char_p 行为不一致