Marshal Deeply Nested Struct Problem!
-
// the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((
-
// the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((
Sorry, just saw some post , currently , struct array as a member of struct is not supported. But use flat stuct , member like this: arr1, arr2.... sounds not a good idea, Who can give a good work around?
-
Sorry, just saw some post , currently , struct array as a member of struct is not supported. But use flat stuct , member like this: arr1, arr2.... sounds not a good idea, Who can give a good work around?
Just to give you an idea, try changing the line in structB From:
[MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )]
To:[MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.LPStruct, SizeConst=4 )]
Basically you may need to specify the type of data that you have in the array. In this case I used LPStruct but you can try other types. I don't understand why we wouldn't be able to pass other structures after all, it's just a bunch of bytes that have to be marshaled into your managed structures. -
// the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((
long in C/C++ is int (Int32) in C#. Also the person who wrote the C code should be shot. You should be able to replace everything with the following:
struct Quote
{
int date;
int price;
int id0;
int id1;
int id2;
int id3;
}void foo(ref Quote quote);
To check do a sizeof(StructC) in C and Marshal.SizeOf(typeof(Quote)) in C#. Both should be 24 bytes. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots