0

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!

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much.It solved this problem perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.