Reputation: 3
I have a C++ API I'm trying to wrap in Python.There is a function Foo(TAPIINT32 &iResult) with typedef int TAPIINT32 in header file.When I use: a=0 mymodule.Foo(a) in python to call it,I get an error as "TypeError: in method 'Foo', argument 1 of type 'TAPIINT32 &'".Is anybody could help with this?Many thanks!
Upvotes: 0
Views: 282
Reputation: 177665
SWIG needs to be told when parameters are outputs and has pre-existing typemaps to help. Here's an example:
api.h
typedef int TAPIINT32;
void Foo(TAPIINT32& iResult);
api.cpp
#include "api.h"
void Foo(TAPIINT32& iResult)
{
iResult = 5;
}
api.i
%module api
%{
#include "api.h"
%}
%include <windows.i>
%apply int* OUTPUT {TAPIINT32t&};
%include "api.h"
The %apply
command tells SWIG to apply an existing typemap to the indicated type. In this case, the pre-existing int* OUTPUT
typemap is applied to all TAPIINT32&
parameters. Note that the OUTPUT typemap suppresses the need for passing a parameter, and returns it as an additional return value instead.
Output:
>>> import api
>>> api.Foo()
5
Upvotes: 1