Marshal.SizeOf

  • Thread starter Thread starter Linus Rörstad
  • Start date Start date
L

Linus Rörstad

Hello!

I'm having problems with getting the size of my own class. The exception I
get is:
Type SocketProtocol.Inparameters can not be marshaled as an unmanaged
structure; no meaningful size or offset can be computed

The class looks like this:
public class Inparameters
{
public enum SyncType
{
Full,
Simple
}
public string id2;
public string id;
public SyncType synctype;
}

Inparameters inparameter = new Inparameters();
inparameter.id = textBox2.Text;
inparameter.synctype = SocketProtocol.Inparameters.SyncType.Full;
int a = Marshal.SizeOf(inparameter);

It's when I call the last line I receive the exception
What am I doing wrong?

Thanks
Linus
 
The marshaller in .NETCF v1.0 can't marshal structures or classes with
strings, depending on how you actually want to marshal the strings you can
either use IntPtr fields which store the address of a native buffer
containing the string, or you create a flat byte array for the entire
structure and store the characters inline up to a maximum length. What are
you passing this structure into?

Peter
 
I'm using it to send to a server application as a byte array. If I use a
char array instead will that work?
 
Fisrt, if you're using it to send a byte array, why not use byte[]? I think
sending a byte array will work if the array length is fixed in the class
declaration. It's probably safer to provide your own operator that provides
a byte array representation of the class, then use it to get the data, so
you're certain it's marshalled the way you want.

-Chris
 
Back
Top