How would you marshall this nested array of structure in C#?
C struct:
typedef struct
{
unsigned int appVersionNumber;
unsigned int networkId;
struct
{
int code;
int endDate;
} profiles[4];
} CardEnvHolder;
My C# attempt:
[StructLayout(LayoutKind.Sequential)]
unsafe struct CardEnvHolder
{
public uint appVersionNumber;
public uint networkId;
public Profiles[] profiles;
}
[StructLayout(LayoutKind.Sequential)]
struct Profiles
{
public int code;
public int endDate;
}
C# Main:
unsafe
{
CardEnvHolder envhold = new CardEnvHolder();
void* ptr = (void*)&envhold; //Error here
EnvHolderTest(ptr);
Console.WriteLine(envhold.profil[1].code);
}
Unfortunately I get the error CS0208 "Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')"
As requested,
EnvHolder C function:
void EnvHoldInit(CardEnvHolder* envhold)
{
envhold->appVersionNumber = 4;
envhold->networkId = 8;
envhold->profiles[1].code = 84;
printf("%d\n", envhold->profiles[1].code);
envhold->profiles[1].code++;
}
EXPORT void EnvHolderTest(void* envhold)
{
EnvHoldInit(envhold);
}
EnvHolder C# prototype:
[DllImport("Sandbox.dll", CallingConvention = CallingConvention.Cdecl)]
extern static unsafe void EnvHolderTest([In, Out] void* envhold);