I have a c++ .dll exporting a function with the following signature:
extern int __stdcall foobar(long ptr, unsigned int *array, unsigned int arraySize);
From c++ Code, I can use it like so:
std::vector<unsigned int> FooBar(6);
int res = foobar(ptr, &FooBar[0], (unsigned int)FooBar.size());
I'd like to use the dll from C# and I've tried this:
[DllImport("mydll.dll")]
public static extern int foobar(long ptr, uint[] arr, uint arrSize);
Call:
uint[] arr = new uint[6];
int count = Obj.foobar(ptr, arr, (uint)arr.GetLength(0)*32)
This throws an "PInvokeStackImbalance" error. What would the correct PInvoke signature look like?
public static extern unsafe Int32 foobar(void* ptr, UInt32* array, UInt32 arraySize)and performing the marshalling yourself?IntPtr. size is wrong too, it is* 4. What it actually means is hard to guess at, raw pointers are usually trouble.extern long __stdcall createPtr(int val);longto .NET isSystem.Int32. Using C#long, which isSystem.Int64, will not work. Alsostd::vector'ssize()member returns the number of elements, not the number of bytes, so multiplyingGetLength(0) * 32is wrong. Not sure where 32 came from to begin with. Just(uint)arr.Lengthwill be correct there.