Andrey Rubliov
Andrey Rubliov

Reputation: 1597

Swig: pass std::vector<unsigned char> to c# function generated from c++

I have a C++ function:

std::map<std::string, std::string> foo(const std::vector<unsigned char>& v);

Swig generates the following C# function:

MapStringString foo(SWIGTYPE_p_std__vectorT_unsigned_char_t v);

I want to call it inside C# function which receives byte[] and returns IDictionary, i.e.:

IDictionary<string, string> bar(byte[] b)
{
  SWIGTYPE_p_std__vectorT_unsigned_char_t b1;

  // initialize b1 from b

  MapStringString m = foo(b1);

  IDictionary<string, string> result;

  // Populate result from MapStringString

  return result;
}

How do I initialize b1 from b (i.e. SWIGTYPE_p_std__vectorT_unsigned_char_t from byte[]) and how to populate IDictionary from MapStringString?

Thanks in advance!

Upvotes: 0

Views: 877

Answers (1)

You can get started quickly with something like this in your SWIG interface:

%include <std_vector.i>
%template(VectorUChar) std::vector<unsigned char>;

That should give you a real type that you can work with from within C# that's a proxy to a C++ std::vector. (It'd work for your bar() function for example, but hardly be seamless at the point of call).

Since you've actually got a Byte[] already you could probably put together a typemap that transparently constructs a vector from a pointer + length if that's more useful.

Upvotes: 1

Related Questions