Hello!
I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this has already been covered.
I have been trying to learn c# as fast as possible in the last month and in doing so i have been re-visiting old C/C++ problems and trying to overcome them in C#.
What i have is a very complex serise of structures. Id rather not change them, as it would cause a lot of work through the rest of the app, however they are causing me problems when it comes to saving the data they contain. My app passes the content of the structures through an encryption algorithem as a byte array, and so here is where the problem starts.
My structures are nested as many as five-deep, all containing strings, string arrays and arrays of other structures (did i mention its a nightmare?)
I have made some head-way with this, and can quite successfully convert a structure to a byte array using the following code-
I have tried many changes to this code, but i always seem to get an error when handling more than one nested structure (I.E struct A contains struct B and struct C) or structures nested more than 1 deep (I.E struct A contains struct B which contains struct C).
The errors i am getting are as follows:-
"Object reference not set to an instance of an object"
- when working with deep nested structures,or the following error :-
"Type ssc.app.structD ata can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
- when working with several structures nested in the same structure
Any solutions to my problem would be of great help (including telling me to rip it all out and start again =))
Kind regards,
I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this has already been covered.
I have been trying to learn c# as fast as possible in the last month and in doing so i have been re-visiting old C/C++ problems and trying to overcome them in C#.
What i have is a very complex serise of structures. Id rather not change them, as it would cause a lot of work through the rest of the app, however they are causing me problems when it comes to saving the data they contain. My app passes the content of the structures through an encryption algorithem as a byte array, and so here is where the problem starts.
My structures are nested as many as five-deep, all containing strings, string arrays and arrays of other structures (did i mention its a nightmare?)
I have made some head-way with this, and can quite successfully convert a structure to a byte array using the following code-
Code:
static byte [] StructureToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte [] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}
The errors i am getting are as follows:-
"Object reference not set to an instance of an object"
- when working with deep nested structures,or the following error :-
"Type ssc.app.structD ata can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
- when working with several structures nested in the same structure
Any solutions to my problem would be of great help (including telling me to rip it all out and start again =))
Kind regards,
Comment