I want to pass an array/vector/buffer of bytes from c++ to c#
On the c++ side I will have some data in a vector:
std::vector<unsigned char> pixels_cpp;
On the c# side I want to access these data in a byte array, eg
byte[] pixels_cs
// or as a MemoryStream object, accompanied with size information.
I am currently using this concept for passing strings from c++ to c#:
void MyWrapper::setSomeString(const std::string& someString)
{
mono_thread_attach (mono_get_root_domain ());
MonoMethod* mmSetSomeString = searchMethod("SetSomeString", monoClass);
void *args [1];
args [0] = mono_string_new (domain, someString.c_str());
MonoObject *result = mono_runtime_invoke (mmSetSomeString, CsClassInstance, args, NULL);
// check result
}
And the c# signature that is called is:
public bool SetSomeString(string someString)
How does this look like if I want to pass a vector of bytes?
The solution is to use mono_array_new. Example:
void MyWrapper::setVector(std::vector<uint8_t>& data)
{
mono_thread_attach (mono_get_root_domain ());
MonoMethod* mmSetVector = searchMethod("SetVector", monoClass);
void *args [1];
MonoArray *data = mono_array_new(domain, mono_get_byte_class(), data.size());
for (auto i=0; i<data.size(); i++) {
mono_array_set (data, uint8_t, i, data[i]);
}
args [0] = data;
MonoObject *result = mono_runtime_invoke (mmSetVector, CsClassInstance, args, NULL);
// check result
}
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.