473,586 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct with fixed size

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(Unman agedType.ByValA rray, 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
6 3284
Erdem,

Try this
[StructLayout(La youtKind.Explic it, 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.n et> wrote in message
news:et******** ******@TK2MSFTN GP15.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(Unman agedType.ByValA rray, 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

"erdem" <er***@kulube.n et> wrote in message
news:et******** ******@TK2MSFTN GP15.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(Unman agedType.ByValA rray, 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
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(Unman agedType.ByValA rray, 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

"erdem" <er***@kulube.n et> wrote in message
news:%2******** *******@TK2MSFT NGP12.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(Unman agedType.ByValA rray, 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(La youtKind.Sequen tial)]
struct T
{
int a;
int b;
int c;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=20)]
char[] name;
}

...
Console.WriteLi ne(Marshal.Size Of(typeof(T)));

Willy.


Nov 16 '05 #5
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(La youtKind.Sequen tial)]
struct T
{
int a;
int b;
int c;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=20)]
char[] name;
}

...
T tt = new T();
tt.a = 5;
tt.b = 6;
tt.c = 7;
tt.name = "12345678901234 56789012".ToCha rArray();
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(La youtKind.Sequen tial)]
struct T
{
int a;
int b;
int c;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=20)]
char[] name;
}

..
Console.WriteLi ne(Marshal.Size Of(typeof(T)));

Willy.

Nov 16 '05 #6

"erdem" <er***@kulube.n et> wrote in message
news:uq******** ******@TK2MSFTN GP12.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(La youtKind.Sequen tial)]
struct T
{
int a;
int b;
int c;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=20)]
char[] name;
}

..
T tt = new T();
tt.a = 5;
tt.b = 6;
tt.c = 7;
tt.name = "12345678901234 56789012".ToCha rArray();
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 = "12345678901234 56789012".ToCha rArray();
copies 22 char to the managed array pointed by name, so, tt.name.Length
correctly returns 22

[MarshalAs(Unman agedType.ByValA rray, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3684
by: Sergey Dorofeev | last post by:
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g. such struct: long, long, some bytes (string), zero, short, short, short.
3
27214
by: Dirk Reske | last post by:
Hello, I have the following struct: public struct WAVEFORMATEX { public UInt16 wFormatTag; //2 bytes public UInt16 nChannels; //2 bytes public UInt32 nSamplesPerSec; //4 bytes public UInt32 nAvgBytesPerSec; //4 bytes
0
1886
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing any members) and marshal the tmp array back to the new struct. The new struct shows the same data. The single type is easy, that just gets copied....
2
7477
by: Cyril | last post by:
Hello, I have a problem to marshal a structure that contains an array of an others struct. This array is an array size fixed (MyStruct myStructs and not MyStruct *myStructs). For example : C declaration : struct Point {
4
2186
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry { public byte MsgType; public uint Tag; public fixed byte ID; public fixed byte Val1;
6
1938
by: Christian Kandeler | last post by:
Hi, I'd like to know whether the following code is standard-compliant: struct generic { int x; int a; };
0
1280
by: O.B. | last post by:
I have the following struct that I'm trying to get to behave like a union, where Unsigned* and Float* are structs. unsafe public struct Bits64 { public fixed Unsigned8 c; public fixed Unsigned16 s; public fixed Unsigned32 u; public fixed Float32 f; public Float64 d;
9
6514
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A structure is defined in the header(.h file) as shown below struct validationResult {
4
8098
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members that represent those fields, so that I can access the records in a meaningful way. Using C, I would have defined a struct that I could cast a byte...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.