python向动态库传结构体数组_Python结构中的动态数组和结构

I am trying to implement this C structures in python using ctypes:

struct _rows {

int cols_count;

char *cols[];

}

struct _unit {

int rows_count;

struct _rows *rows;

}

int my_func(struct _unit *param);

Problem is that _rows.cols is a dynamically sized array of char pointer and _unit.rows is a dynamically sized array of _rows structure. How can I implement this using ctypes in python?

I was able to define a function that will return a _rows structure with variable number of char pointers:

def get_row(cols):

class Row(ctypes.Structure):

_fields_ = [("cols_count", ctypes.c_int),

("cols", ctypes.c_char_p * cols)

]

I don't know what to do nex, it's all a bit fuzzy and ctypes documentation isn't helping.

解决方案

I'm making some assumptions about what the OP wants, and I'd love suggestions if there is an easier way to do this, but this is what I came up with:

demo.py

import string

from ctypes import Structure,c_int,c_char_p,POINTER,cast,pointer,byref,CDLL

class Row(Structure):

_fields_ = [('cols_count', c_int),

('cols', POINTER(c_char_p))]

def __init__(self,cols):

self.cols_count = cols

# Allocate an array of character pointers

pc = (c_char_p * cols)()

self.cols = cast(pc,POINTER(c_char_p))

class Unit(Structure):

_fields_ = [('rows_count', c_int),

('rows',POINTER(Row))]

def __init__(self,rows,cols):

self.rows_count = rows

# Allocate an array of Row structures.

# This does NOT call __init__.

pr = (Row * rows)()

# Call init manually with the column size.

for r in pr:

r.__init__(cols)

self.rows = cast(pr,POINTER(Row))

unit = Unit(2,3)

# Stuff some strings ('aaaaa','bbbbb',etc.)

for i in xrange(unit.rows_count):

for j in xrange(unit.rows[i].cols_count):

unit.rows[i].cols[j] = string.ascii_lowercase[i*5+j]*5

dll = CDLL('test.dll')

dll.my_func(byref(unit))

test.c

#include

struct _rows {

int cols_count;

char **cols;

};

struct _unit {

int rows_count;

struct _rows *rows;

};

__declspec(dllexport) int my_func(struct _unit *param)

{

int i,j;

for(i=0;irows_count;i++)

for(j=0;jrows[i].cols_count;j++)

printf("%d,%d = %s\n",i,j,param->rows[i].cols[j]);

return 0;

}

makefile

Compiled with Visual Studio 2010.

test.dll: test.c

cl /W4 /LD test.c

Output

0,0 = aaaaa

0,1 = bbbbb

0,2 = ccccc

1,0 = fffff

1,1 = ggggg

1,2 = hhhhh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值