痛饮的PHP包装使用指针,C code是一个数组 [英] swig-php wrapper uses pointer, c code is an array

查看:124
本文介绍了痛饮的PHP包装使用指针,C code是一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用痛饮生成一个PHP扩展调用到一个C共享库。我能得到最多的事除了以下情况,工作...

I am using SWIG to generate a PHP extension that calls into a 'c' shared lib. I am able to get most things to work except the following situation...

在我的'C'code我宣布一个函数(请注意,结构和功能的名称已更改为保护无辜者):

In my 'c' code I declare a function as (Please note that structure and function names have been changed to protect the innocent):

int getAllThePortInfo(EthernetPort *ports);

在此情况下,参数的端口的实际上是EthernetPort结​​构的阵列。在另一个'C'节目,我可以把它称为像这样...

In this case, the parameter ports is actually an array of EthernetPort structures. In another 'c' program, I could have called it like this...

EthernetPort ports[4];
int rval = getAllThePortInfo(ports);
<etc>
<etc>

这工作正常。然后我跑痛饮,我生成共享库,以及所有建立良好。我让PHP code,我可以打电话...

This works fine. Then I run SWIG, generate my shared lib, and all builds well. I get php code that I can call...

$ports = new_ethernetport();
$rval = getAllThePortInfo($ports);

这会导致PHP抛出以下错误: PHP:免费():无效的指针:0x099cb610

This causes PHP to throw the following error : php: free(): invalid pointer: 0x099cb610

所以,我试图做这样的事情...

So, I tried to do something like...

$ports = array(new_ethernetport(), new_ethernetport(), new_ethernetport(), new_ethernetport());
$rval = getAllThePortInfo($ports);

但随后PHP抱怨......
PHP致命错误:在getAllThePortInfo的参数1类型错误。预计SWIGTYPE_p_EthernetPort

我认为正在发生的是,PHP(和痛饮)不指针与数组之间的区别,并在包装​​,它是想指向一个单一结构的时候,在现实中,它是一个结构数组。

What I think is happening is that PHP (and SWIG) do not differentiate between pointers and arrays, and in the wrapper, it is thinking 'pointer to a single structure', when, in reality, it is an array of structures.

有什么在PHP中我能做什么?分配的内存块,我可以为存储多个结构的空间使用?

Is there something in PHP I can do? Allocate a chunk of memory that I can use as a space to store more than one structure?

是否与痛饮东西我可以做,使我的包装明白了我的意图更好?

Is there something with SWIG I can do to make my wrapper understand my intentions better?

我真的会AP preciate任何建议。谢谢你。

I truly would appreciate any suggestions. Thanks.

推荐答案

carrays.i答案的确抱到我的问题...

carrays.i indeed hold the answer to my question...

在痛饮接口文件,我有以下行...

In the SWIG interface file, I have the following lines...

%include <carrays.i>
%array_functions(EthernetPort, ethernetPortArray);
%include "my_lib.h"

my_lib.h包含EthernetPort结​​构的typedef,以及函数声明...

"my_lib.h" contains the typedef for the EthernetPort structure, as well as the function declaration...

#define NUM_ETHERNET_PORTS 4

typedef struct {
    int number;
    mode_t mode;
} EthernetPort;

int getAllThePortInfo(EthernetPort *ports);

运行SWIG和建立共享库后的 my_lib.so ,我可以使用下面的PHP code ...

After running SWIG and building the shared lib my_lib.so, I can use the following PHP code...

$ports = new_ethernetPortArray(NUM_ETHERNET_PORTS);
$rval = getAllThePortInfo($ports);

$port0 = ethernetPortArray_getitem($ports, 0);
$pnum = ethernetport_number_get($port1);
$pmode = ethernetport_mode_get($port1);

// port1 port2 port3 etc etc 

delete_ethernetPortArray($ports);

PHP函数的 new_ethernetPortArray ethernetPortArray_getitem ethernetport_number_get ethernetport_mode_get delete_ethernetPortArray 全部由痛饮基础上创建的.i文件。

The php functions new_ethernetPortArray, ethernetPortArray_getitem, ethernetport_number_get, ethernetport_mode_get, delete_ethernetPortArray were all created by SWIG based on the .i file.

另一个好处SWIG能是利用#在我的PHP code(例如NUM_ETHERNET_PORTS)定义的,让我为我的一些常见的数据一个地方。我喜欢。 : - )

The other benefit SWIG enables is the use of #define's in my php code (e.g. NUM_ETHERNET_PORTS), allowing me a single place for some of my common data. I like that. :-)

干杯。

这篇关于痛饮的PHP包装使用指针,C code是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆