By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,892 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,892 IT Pros & Developers. It's quick & easy.

struct with fixed size

P: n/a
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...
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"erdem" <er***@kulube.net> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
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...

Nov 16 '05 #2

P: n/a

"erdem" <er***@kulube.net> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 16 '05 #3

P: n/a
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
Nov 16 '05 #4

P: n/a

"erdem" <er***@kulube.net> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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.


Nov 16 '05 #5

P: n/a
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...

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.

Nov 16 '05 #6

P: n/a

"erdem" <er***@kulube.net> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.