将C结构传递给C DLL中的函数

时间:2013-10-31 19:23:40

标签: python c dll ctypes

我创建了一个名为"名为"的.DLL使用其中的一些实用程序函数的共享内存

我需要使用具有以下原型的函数通过python在C struct之后编写/读取:

  • int write_shmem(const char* shmem_name, char* shmem)
  • int read_shmem ( const char* shmem_name , char* shmem);

.DLL文件

  • shmem_name是共享内存名称
  • shmem是要写的数据

C struct类似于

typedef struct {
   unsigned char c;
   unsigned long l;
   float f;
   double d;
 } DataBlock ;

我在python代码中使用了以下内容

from ctypes import *
dll = cdll.LoadLibrary('shared_mem.dll')
write_shmem = dll.write_shmem
write_shmem.restype = ctypes.c_int

read_shmem = dll.read_shmem
read_shmem.restype = ctypes.c_int

class DataBlock(Structure):
    _fields_ = [('c', c_ubyte), ('l', c_ulong),
                ('f',c_float), ('d', c_double) ]

data = DataBlock('A',123, 12.32, 1.89721)

write_shmem(c_char_p(b'P0W') , ??? ) #  cast(data, POINTER(c_char) ?

#...

read_shmem(c_char_p(b'P0W'),  ??? )  #  cast(data, POINTER(c_char) ?

如何对char*进行类型转换?

使用强制转换是否会将数据正确地放入共享内存?我需要从C应用程序中读取相同的共享内存

修改

使用:

来自int create_shmem(const char*, long long );的{​​p> .DLL我正在创建一个名为"的名字"共享内存

Python代码:

create_shmem( c_char_p(b'P0W'),
               ctypes.c_longlong(sizeof(DataBlock)) )

write_shmemread_shmem将使用创建的内存大小简单地写入/读取数据。这是在.DLL函数本身中完成的。 (此处使用Boost进程间共享内存对象)

使用Python版本:3.3.0

1 个答案:

答案 0 :(得分:5)

我没有read_shmemwrite_shmem个版本的功能。所以我创建了以下虚拟版本:

#include <stdio.h>

typedef struct {
    unsigned char c;
    unsigned long l;
    float f;
    double d;
} DataBlock ;

int write_shmem(const char* shmem_name, char* data)
{
    DataBlock* block = (DataBlock*)data ;
    printf("%c %ld %f %lf\n", block->c, block->l, block->f, block->d) ;
    return sizeof(DataBlock) ;
}

int read_shmem(const char* shmem_name, char* data) 
{
    DataBlock* block = (DataBlock*)data ;
    block->c = 'z' ;
    block->l = 3 ;
    block->f = block->d = 3.14 ;
    return sizeof(DataBlock) ;
}

在Python代码中,我可以这样称呼它们:

#! /usr/bin/env python3
from ctypes import *
dll = cdll.LoadLibrary('./libshmem.so')

write_shmem = dll.write_shmem
write_shmem.restype = c_int

read_shmem = dll.read_shmem
read_shmem.restype = c_int

class DataBlock(Structure):
    _fields_ = [('c', c_ubyte), ('l', c_ulong),
                ('f',c_float), ('d', c_double) ]

#
# Using byref()
#
data = DataBlock(ord('A'), 123, 12.32, 1.89721)

write_shmem(b"P0W", byref(data))
read_shmem(b"P0W", byref(data))

print("{0} {1} {2} {3}".format(chr(data.c), data.l, data.f, data.d))

#
# Using pointer()
#
data = DataBlock(ord('A'), 123, 12.32, 1.89721)

write_shmem(b"P0W", pointer(data))
read_shmem(b"P0W", pointer(data))

print("{0} {1} {2} {3}".format(chr(data.c), data.l, data.f, data.d))

#
# Using cast()
#
data = DataBlock(ord('A'), 123, 12.32, 1.89721)

write_shmem(b"P0W", cast(pointer(data), c_char_p))
read_shmem(b"P0W", cast(pointer(data), c_char_p))

print("{0} {1} {2} {3}".format(chr(data.c), data.l, data.f, data.d))

每种情况下的输出(byrefpointercast用法)为:

A 123 12.320000 1.897210
z 3 3.140000104904175 3.14

我正在使用Linux,因此我使用以下Makefile从C代码创建共享对象库:

libshmem.so: dummy_shmem.o
    gcc -shared -o libshmem.so dummy_shmem.o

dummy_shmem.o: dummy_shmem.c
    gcc -fpic -c dummy_shmem.c

但是Windows上的行为应该大致相同。

未经请求的建议

请注意,使用write_shmemread_shmem隐式了解传递给它们的数据缓冲区的大小是危险的。如果您不小心将另一种类型的对象传递给它们,您将得到奇怪的结果。更安全的选择可能是围绕这些函数创建填充程序以显式管理DataBlock实例。这是一个我有create_shmemwrite_shmemread_shmem虚拟实现的示例:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct {
    unsigned char c;
    unsigned long l;
    float f;
    double d;
} DataBlock ;

static char* dummy_shmem = 0 ;
static size_t shmem_size = 0 ;

int create_shmem(const char* shmem_name, const size_t data_size)
{
    free(dummy_shmem) ;
    dummy_shmem = 0 ;
    shmem_size = 0 ;

    dummy_shmem = malloc(data_size) ;
    shmem_size = dummy_shmem ? data_size : 0 ;

    return shmem_size ;
}

int write_shmem(const char* shmem_name, const char* data, const size_t data_size)
{
    if (data_size <= shmem_size) {
        memcpy(dummy_shmem, data, data_size) ;
        return data_size ;
    }
    return 0 ;
}

int read_shmem(const char* shmem_name, char* data, const size_t data_size) 
{
    if (data_size <= shmem_size) {
        memcpy(data, dummy_shmem, data_size) ;
        return data_size ;
    }
    return 0 ;
}

int create_block(const char* shmem_name)
{
    printf("create_block: %s\n", shmem_name) ;
    return create_shmem(shmem_name, sizeof(DataBlock)) ;
}

int write_block(const char* shmem_name, const DataBlock* data)
{
    printf("write_block: [%s] %c %ld %f %lf\n", shmem_name, data->c, data->l, data->f, data->d) ;
    return write_shmem(shmem_name, (char*)data, sizeof(DataBlock)) ;
}

DataBlock read_block(const char* shmem_name)
{
    DataBlock result ;
    read_shmem(shmem_name, (char*)&result, sizeof(DataBlock)) ;
    printf("read_block: [%s] %c %ld %f %lf\n", shmem_name, result.c, result.l, result.f, result.d) ;
    return result ;
}

然后Python代码变得更简单,更清晰,更安全:

#! /usr/bin/env python3
from ctypes import *
dll = cdll.LoadLibrary('./libshmem.so')

class DataBlock(Structure):
    _fields_ = [('c', c_ubyte), ('l', c_ulong),
                ('f',c_float), ('d', c_double) ]

create_block = dll.create_block
create_block.argtypes = [c_char_p]
create_block.restype = c_int

write_block = dll.write_block
write_block.argtypes = [c_char_p, POINTER(DataBlock)]
write_block.restype = c_int

read_block = dll.read_block
read_block.argtypes = [c_char_p]
read_block.restype = DataBlock
#
# Create memory block 
#
create_block(b"P0W")    
#
# Write data block to shmem
#
in_data = DataBlock(ord('A'), 123, 12.32, 1.89721)
write_block(b"P0W", in_data)
#
# Read data block from shmem. 
#
out_data = read_block(b"P0W")

print("{0} {1} {2} {3}".format(chr(in_data.c), in_data.l, in_data.f, in_data.d))
print("{0} {1} {2} {3}".format(chr(out_data.c), out_data.l, out_data.f, out_data.d))

更新

  • 通过引用DataBlock传递write_block(请参阅下面的eryksun评论)。