3

I'm using SWIG to create a C# wrapper for a C++ library that I don't own.

The only functions that I've got problems with look like:

int getStringWrapper(..., StringWrapperClass& parameter);

If it helps, the StringWrapperClass is not owned by me, but looks quite similar to the StringPtr class mentioned in this question. The linked question was related to std::string but I think my problem is more general.

How do you write a SWIG typemap (for C#) for a method that takes a non constant reference to a class type?

SWIG is generating:

SWIGEXPORT int SWIGSTDCALL CSharp_getStringWrapper(..., void * jarg3) {
  int jresult ;
  ...
  StringWrapperClass *arg3 = 0 ;
  int result;

  ...
  arg3 = (StringWrapperClass *)jarg3;
  if (!arg3) {
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "StringWrapperClass & type is null", 0);
    return 0;
  } 
  result = (int)getStringWrapper(...,*arg3);
  jresult = result; 
  return jresult;
}

but I am getting a protected memory access error at the line where it calls getStringWrapper.

I am not very good at C++... as far as I can tell, SWIG is using a pointer StringWrapperClass* but it needs a reference and this is causing the problem?

I have succesfully wrapped other methods that have a signature like:

int getStringWrapper(..., StringWrapperClass*& parameter);

In this case the SWIG generated code was similar to the above but with:

arg3 = (StringWrapperClass**)jarg3;

I've looked at this question, which looks pretty much like what I am trying to achieve except that I need this in C#. The linked question, mentioned earlier, from that post also seems to provide a solution. I've tried to write my own C# typemaps along these lines, for before realising that I don't know how to replace all the jenv, JCALL1 stuff for C#.

There's also this answer but I don't really understand what I'm aiming for right now.

Please can somebody with more understanding of SWIG and C++ than me point me in the right direction?

Community
  • 1
  • 1
SWilliams
  • 691
  • 7
  • 28

0 Answers0