❔ Need help with C#/C++ interop

I have a C++ dll (that I can't change or port to C#) that exports this function
void func(const unsigned char* in, unsigned char* out, int size)
void func(const unsigned char* in, unsigned char* out, int size)
I have some C# code that obtains a size, creates an input byte[] array holding "size" bytes of data and then creates an output byte[] array with new byte[size]. How would I declare my C++ function in C# and how would I then call it (passing in the byte arrays and size so the C++ dll gets what it expects, i.e. a pair of pointers to raw bytes it can read from and write to plus the size of those arrays)
4 Replies
ero
ero17mo ago
[DllImport("")]
static extern void func(byte[] @in, byte[] @out, int size);
[DllImport("")]
static extern void func(byte[] @in, byte[] @out, int size);
pretty straight forward to be more correct though, maybe
[DllImport("")]
static extern void Func(byte* @in, byte* @out, int size);
[DllImport("")]
static extern void Func(byte* @in, byte* @out, int size);
Jonathan Wilson
Jonathan WilsonOP17mo ago
Ok thanks.
nukleer bomb
nukleer bomb17mo ago
Also you can use refs to avoid unsafe context and specify data direction:
[DllImport("")]
static exrern void Func(in byte @in, ref byte @out, int size);
[DllImport("")]
static exrern void Func(in byte @in, ref byte @out, int size);
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server