Marshal.PtrToStructure problem !!



I have two C++ structures like these :

typedef struct answer_series_item
int32_t contract_size_i;
int32_t price_quot_factor_i;
char ins_id_s [32];
char isin_code_s [12];
uint8_t suspended_c;
char date_last_trading_s [8];
char time_last_trading_s [6];
char settlement_date_s [8];
char start_date_s [8];
char end_date_s [8];
char date_delivery_start_s [8];
char date_delivery_stop_s [8];
uint8_t series_status_c;
} answer_series_item_t;

typedef struct answer_series
uint16_t segment_number_n;
uint16_t items_n;
answer_series_item_t item [400];
} answer_series_t;

The first is present 400 times in the second one.

I have create in C# these two structures but when I try to Marshar I
rise an error.

C# Structures
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class answer_series_t
[MarshalAs(UnmanagedType.I2)] public Int16 segment_number_n;
[MarshalAs(UnmanagedType.I2)] public Int16 items_n;
[MarshalAs(UnmanagedType.Struct, SizeConst=6)] public
answer_series_item_t [] item = new answer_series_item_t[6];

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class answer_series_item_t
[MarshalAs(UnmanagedType.I4)] public Int32 contract_size_i;
[MarshalAs(UnmanagedType.I4)] public Int32 price_quot_factor_i;
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =32)] public char
[]ins_id_s = new char[32];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =12)] public char
[]isin_code_s = new char[12];
[MarshalAs(UnmanagedType.U1)] public char suspended_c;
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]date_last_trading_s = new char[8];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =6)] public char
[]time_last_trading_s = new char[6];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]settlement_date_s = new char[8];/
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]start_date_s = new char[8];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]end_date_s = new char[8];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]date_delivery_start_s = new char[8];
[MarshalAs(UnmanagedType.ByValArray ,SizeConst =8)] public char
[]date_delivery_stop_s = new char[8];
[MarshalAs(UnmanagedType.I1)] public char series_status_c;

From the C++ DLL I have the memory pointer.

I use this command :

Marshal.PtrToStructure(mypointer, mystructure);

Where mypointer is the pointer from C++ and mystructure is a new
istance of the c# structure.

The error message is (sorry but is in Italian..):

Impossibile effettuare il marshalling del campo item del tipo
answer_series_t. Non è possibile effettuare il marshalling di questo
tipo come campo struttura.

Where is my mistake ? :evil:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question