使用 Swig/Python 在 C 中传递多个参数和分配字符串 [英] Passing multiple parameters and allocating strings in C using Swig/Python

查看:41
本文介绍了使用 Swig/Python 在 C 中传递多个参数和分配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SWIG 包装以下 C 接口以从 Python 访问它:

I am using SWIG to wrap the following C interface to access it from Python:

void some_method(char **output, int paramA, const char *paramB, int paramC);

C 中的实现在运行时使用 malloc() 为指针 *output 分配内存.其他参数是只读的,用于传达方法所需的附加信息.

The implementation in C allocates memory at runtime using malloc() to the pointer *output. The other parameters are read-only and convey additional information needed by the method.

对应的 SWIG 接口文件应该是什么样子的?

What should the corresponding SWIG interface file look like?

如果我只传递在 C 中动态分配的输出"参数,而不传递其他参数,则这种情况非常简单.即如果我的 C 接口如下并且它的实现是 example.c(比如说):

This case is very simple if I passed only the 'output' parameter which is dynamically allocated in C, and not the other parameters. i.e. If my C interface was the following and its implementation is example.c (say):

void some_method(char **output);

然后 SWIG 接口文件很简单,如这个stackoverflow线程中所述:

Then the SWIG interface file is simple, as explained in this stackoverflow thread:

%module example
%include<cstring.i>
%cstring_output_allocate(char **output, free(*$1));
%{  
    extern void some_method(char **output);
%}
%include example.c

以上不适用于多个参数.如何传递多个参数,以及允许动态分配其中一个参数(在本例中为输出"参数).

The above does not work with multiple parameters. How can one pass multiple parameters, as well as allow dynamic allocation for one of the parameters (in this case 'output' parameter).

推荐答案

%cstring_output_allocate 确实处理多个参数.它声明如果您看到 char **output 作为 any 方法的参数,请隐藏该参数并将其作为附加输出返回.

%cstring_output_allocate does work with multiple parameters. It declares "If you see char **output as a parameter to any method, hide the parameter and return it as an additional output.

这是一个例子.我声明了两种方法:一种返回值,一种不返回值.请注意在输出中如何不传递 output 参数,但它的结果作为附加返回值返回.

Here's an example. I declare two methods: one that returns a value and one that doesn't. Note in the output how the output parameter is not passed, but it's result is returned as an additional return value.

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

void some_method(char **output, int paramA, const char *paramB, int paramC)
{
    *output = malloc(paramA);
    sprintf_s(*output,paramA,"%s_%d",paramB,paramC);
}

int some_method2(char **output, int paramA, const char *paramB, int paramC)
{
    *output = malloc(paramA);
    sprintf_s(*output,paramA,"%s_%d",paramB,paramC);
    return strlen(*output);
}

x.h

void some_method(char **output, int paramA, const char *paramB, int paramC);
int some_method2(char **output, int paramA, const char *paramB, int paramC);

x.i

%module x

%begin %{
#pragma warning(disable:4100 4127 4211 4706)
%}

%{
#include "x.h"
%}

%include<cstring.i>
%cstring_output_allocate(char **output, free(*$1));
%include "x.h"

制作文件

_x.pyd: x.c x_wrap.c x.h
    cl /LD /W4 /MD /Ic:\python27\include x.c x_wrap.c -link /LIBPATH:c:\python27\libs

x_wrap.c: x.i x.h
    swig -python x.i

输出

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import x
>>> x.some_method(100,'blah',123)
'blah_123'
>>> x.some_method2(100,'blah',123)
[8, 'blah_123']

这篇关于使用 Swig/Python 在 C 中传递多个参数和分配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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