I have two C++ structures that I have to send as parameters when calling a DLL method from C#.
For example, lets define them as:
struct A
{
int data;
}
struct B
{
int MoreData;
A * SomeData;
}
A method that I need to call from C# has the following signature:
int operation (B * data);
(Please note that I do not have the control over these C++ structures nor methods. )
In C# I define these structures as classes:
[StructLayout(LayoutKind.Sequential)]
class A
{
public int data;
}
[StructLayout(LayoutKind.Sequential)]
class B
{
public int MoreData;
[MarshalAs(UnmanagedType.Struct)]
public A SomeData;
}
I have created a "debugging dll" to call from C# in order to ensure all the data is received correctly in C++ methods. So far only the data that comes before the nested structure pointer is sent correctly.
When I try to read data from the nested structure (B->A->data) I get a read violation error (AccessViolationException).
How can I marshal the nested structure so I will be able to read it in the C++ method?