drewcare
March 13th, 2007, 05:12 PM
I'm using .Net 2.0.50727 in visual studio 2005.
I'm trying to convert some C struct I have into a C# structure (to use for a cast from a byte*) and ran into some trouble when I wanted to create an array of structs inside of a struct like this:
From C:
typedef struct {
float a;
float b;
float c;
} myStruct1;
typedef struct {
float a;
myStruct1 b[100];
} myStruct2;
Via some googling and messing around, I tried doing the following (which compiles)
struct myStruct1
{
public float a;
public float b;
public float c;
}
unsafe struct myStruct2
{
public float a;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.LPStruct, SizeConst = 100)]
pubic MyStruct1[] b;
}
Like I said, this compiles. The problem I have is when I try and use the MyStruct2 structure in a method:
private unsafe void(MyStruct2* pMs2) { ... }
This creates the following compiler error:
Cannot take the address of, get the size of, or declare a pointer to a managed type ('MyStruct2')
Does anyone have any ideas (or workarounds) that doesn't involve changing the structure (which is not an option)? I'm new to C#, so there might be some obvious thing that I've missed or something, but I've googled for an answer and haven't found one yet.
I'm trying to convert some C struct I have into a C# structure (to use for a cast from a byte*) and ran into some trouble when I wanted to create an array of structs inside of a struct like this:
From C:
typedef struct {
float a;
float b;
float c;
} myStruct1;
typedef struct {
float a;
myStruct1 b[100];
} myStruct2;
Via some googling and messing around, I tried doing the following (which compiles)
struct myStruct1
{
public float a;
public float b;
public float c;
}
unsafe struct myStruct2
{
public float a;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.LPStruct, SizeConst = 100)]
pubic MyStruct1[] b;
}
Like I said, this compiles. The problem I have is when I try and use the MyStruct2 structure in a method:
private unsafe void(MyStruct2* pMs2) { ... }
This creates the following compiler error:
Cannot take the address of, get the size of, or declare a pointer to a managed type ('MyStruct2')
Does anyone have any ideas (or workarounds) that doesn't involve changing the structure (which is not an option)? I'm new to C#, so there might be some obvious thing that I've missed or something, but I've googled for an answer and haven't found one yet.