Marshall an array of structs
I am calling a C++ function from C#. As arguments it receives a pointer to an array of structs.
struct A
{
int data;
}
int CFunction (A* pointerToFirstElementOfArray, int NumberOfArrayElements)
In C# I have created the same 开发者_Go百科struct (as a class) and I marshall it correctly (the first element in the array is received correctly). Here is my C# definition of the C++ struct:
[StructLayout(LayoutKind.Sequential), Serializable]
class A
{
int data;
}
The first element is read correctly so I know all the fields in the struct are marshalled correctly. The problem occurs when I try to send over an array of elements. How do I create an array of classes that will be in a single memory block (chunk), so the C++ function can increment the pointer to the array?
I guess I would need something similar to stackalloc, however I belive that only works for primitive types?
Thank you for your help.
This is not intended to be a definitive answer, but merely an example of you could accomplish this using stackalloc
and unsafe code.
public unsafe class Example
{
[DllImport("library.dll")]
private static extern int CFunction(A* pointerToFirstElementOfArray, int NumberOfArrayElements);
public void DoSomething()
{
A* a = stackalloc A[LENGTH];
CFunction(a, LENGTH);
}
}
Also, pay attention to the packing of the struct that the API accepts. You may have to play around with the Pack
property of the StructLayout
attribute. I believe the default is 4, but some APIs expect 1.
Edit:
For this to work you will have to change the declaration of A
from a class to a struct.
public struct A
{
public int data;
}
精彩评论