Click to See Complete Forum and Search --> : Array of struct inside of a struct


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.

darwen
March 14th, 2007, 01:27 AM
Is this method a DllImport-ed method ? If not, there's no point in passing a pointer to your structure in this way.

Use the below method.


private void MyFunction(ref MyStruct2 msg) { ... }


If you are DllImporting then you should be able to just pass the same as above and C# will take care of the marshalling for you.

Generally speaking I don't use 'unsafe' code any more in C# : rather I just the GCHandle class to pin the memory and then pass its address around. And this is only when the default marshalling for P/Invoke fails me.

Darwen.

drewcare
March 14th, 2007, 08:06 AM
I'm doing this because I'm receiving a stream of bytes over a tcp/ip connection. On the server side (C) the data is in a structure (like on my first post). I need a way to take these bytes and access them via some structure in C#.

So... my socket receive gets a byte[] which I cast to a byte* and then cast it to a structure of sorts (like a myStruct2*).

Is there a better way to do this?

drewcare
March 14th, 2007, 01:18 PM
Is this method a DllImport-ed method ? If not, there's no point in passing a pointer to your structure in this way.

Use the below method.


private void MyFunction(ref MyStruct2 msg) { ... }


If you are DllImporting then you should be able to just pass the same as above and C# will take care of the marshalling for you.

Generally speaking I don't use 'unsafe' code any more in C# : rather I just the GCHandle class to pin the memory and then pass its address around. And this is only when the default marshalling for P/Invoke fails me.

Darwen.

I tried this, and had to call the method like this:


MyFunciton(ref *(MyStruct2*)pBytes));


where pBytes is a byte*

I get the same error:

Cannot take the address of, get the size of, or declare a pointer to a managed type ('MyStruct2')

Its the same underlying problem: I need a structure of the exact same size as the one on the C side (who sends this data to me) having a fixed array of another struct.