简体   繁体   中英

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, ie:

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 (ie SWIGTYPE_p_std__vectorT_unsigned_char_t from byte[]) and how to populate IDictionary from MapStringString?

Thanks in advance!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM