struct with fixed size

E

erdem

hi all,

i guess this question had been asked many times but i couldnt find a way

how can we generate a struct with managed code with fixed size

i mean

struct easy
{
int a,b,c;
char[] name;
//or it can be string also
// string name;
}

i want all these objects type of easy have size of sizeof(int)*3 (3
variable) + 20 byte for name

i tried this code but it didnt worked out

struct easy
{
int a,b,c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
char[] name;
//or it can be string also
// string name;
}

it didnt throw any exception but it also didnt worked

how it can be done???

thanks in advance

erdem...
 
P

Philip Coveney

Erdem,

Try this
[StructLayout(LayoutKind.Explicit, Size=32)]
internal struct easy
{
[FieldOffset(0)] int a;
[FieldOffset(4)] int b;
[FieldOffset(8)] int c;
[FieldOffset(12)] char[] name;
}
PC
 
W

Willy Denoyette [MVP]

erdem said:
hi all,

i guess this question had been asked many times but i couldnt find a way

how can we generate a struct with managed code with fixed size

i mean

struct easy
{
int a,b,c;
char[] name;
//or it can be string also
// string name;
}

i want all these objects type of easy have size of sizeof(int)*3 (3
variable) + 20 byte for name

i tried this code but it didnt worked out

struct easy
{
int a,b,c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
char[] name;
//or it can be string also
// string name;
}

it didnt throw any exception but it also didnt worked

how it can be done???

thanks in advance

erdem...

We would love to help you, but first you have to tell us what exactly didn't
work.

Willy.
 
E

erdem

Willy hi,
the thing is
i want to allocate same amount of memory for all instances of struct
exactly when i create instance of struct.

but i cant define char array like

char name[20] in struct

i can only write
as i wrote before
struct easy
{
int a,b,c;
char[] name;
}

size of name is not defined
but i want to be sure that size of all instances of this struct
will be
a,b,c (int) + name (char)
4byte*3 + 20 byte = 32 byte

again, i tried this [MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
but again it didnt allocated 20 byte for char array,

i dont know is it clear enough but...

thanks
 
W

Willy Denoyette [MVP]

erdem said:
Willy hi,
the thing is
i want to allocate same amount of memory for all instances of struct
exactly when i create instance of struct.

but i cant define char array like

char name[20] in struct

i can only write
as i wrote before
struct easy
{
int a,b,c;
char[] name;
}

size of name is not defined
but i want to be sure that size of all instances of this struct
will be
a,b,c (int) + name (char)
4byte*3 + 20 byte = 32 byte

again, i tried this [MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
but again it didnt allocated 20 byte for char array,

i dont know is it clear enough but...

thanks

Not sure how you checked the marshaled size of strucure, but executing this
returns 32.

[StructLayout(LayoutKind.Sequential)]
struct T
{
int a;
int b;
int c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
char[] name;
}

...
Console.WriteLine(Marshal.SizeOf(typeof(T)));



Willy.
 
E

erdem

Willy hi again,

yes you are right, it allocates 20 byte for char array

i didnt checked it because i tried to assign char array that is 22 bytes
long and it didnt caused any problem and so i told it didnt allocates 20
byte.
Now can you look at this code

[StructLayout(LayoutKind.Sequential)]
struct T
{
int a;
int b;
int c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
char[] name;
}

...


T tt = new T();
tt.a = 5;
tt.b = 6;
tt.c = 7;
tt.name = "1234567890123456789012".ToCharArray();
int i = Marshal.SizeOf(tt);
MessageBox.Show(i.ToString() + " " + tt.name.Length.ToString());

Console : 32 (yes that's right 12 + 20 = 32 bytes) 22 (nop it should
be 20 bytes long altough it is 22 bytes long there is no exception or
something else vs vs)

is this correct, or am i missing something ??

(or SizeConst=20 means that if i try to assign this way:

tt.name[0] = 'a';
....
tt.name[19] = 'p';
tt.name[20] = 'x' //no exception)

thank you for your interest

erdem...
 
W

Willy Denoyette [MVP]

erdem said:
Willy hi again,

yes you are right, it allocates 20 byte for char array

i didnt checked it because i tried to assign char array that is 22 bytes
long and it didnt caused any problem and so i told it didnt allocates 20
byte.
Now can you look at this code

[StructLayout(LayoutKind.Sequential)]
struct T
{
int a;
int b;
int c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
char[] name;
}

..


T tt = new T();
tt.a = 5;
tt.b = 6;
tt.c = 7;
tt.name = "1234567890123456789012".ToCharArray();
int i = Marshal.SizeOf(tt);
MessageBox.Show(i.ToString() + " " + tt.name.Length.ToString());

Console : 32 (yes that's right 12 + 20 = 32 bytes) 22 (nop it should be
20 bytes long altough it is 22 bytes long there is no exception or
something else vs vs)

is this correct, or am i missing something ??


Yes it's correct,
tt.name = "1234567890123456789012".ToCharArray();
copies 22 char to the managed array pointed by name, so, tt.name.Length
correctly returns 22

[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
tells the marshaler to copy only 20 char from the managed array to the
marshaling buffer when passing this structure to unmanaged code.

Marshal.SizeOf(tt);

returns the marshaled length in bytes of the struct tt which is 32.

I'm still not clear why you need a struct, are you needing this in an
interop scenario?

Willy.
 

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

Top