I have a method defined in an unmanaged DLL:
int foo(somestruct * vector, int size)
How can I call this method from C#?
Essentially, I want to complete the following snippet in my code:
[StructLayout(LayoutKind.Sequential), Serializable]
public struct somestruct
{
//Whatever.
};
[DLLImport("some.dll")]
public static extern int foo( ???? );
Thanks.
It’s much simpler than you think.
The one thing to consider is marshaling direction. The runtime makes some decisions on whether to marshal the array before the call, after the call, or both. To make sure it makes the right decision, you may want to use
Inand/orOutattributes.On the other hand, if
somestructis blittable, then no marshaling is necessary, as the runtime can just pin the array and pass a pointer to the managed copy.