| | |
Fixed size structure in C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
Hello,
How can I define fixed size of structure in c#..
e.g.
Here i am not able to get the sizeof abc as 14 ..and got error while mashal.sizeof(abc);
How should i preallocate memory of size of structure ?
I can easily does that in c/c++ by just declaring it .. how to make this possible in c#?
and i can initialise the structure using memset in c .. whats its alternative in c#?
It will be helpful to get the solution
How can I define fixed size of structure in c#..
e.g.
C# Syntax (Toggle Plain Text)
struct xyz{ char localc[12]; short localshort; }; I had made a c# structure like this [StructLayout(LayoutKind.Sequential, Pack = 2,CharSet=CharSet.Ansi, Size = 14)] struct xyz{ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public char[] localc; short localshort; } xyz abc = new xyz();
How should i preallocate memory of size of structure ?
I can easily does that in c/c++ by just declaring it .. how to make this possible in c#?
and i can initialise the structure using memset in c .. whats its alternative in c#?
It will be helpful to get the solution
Import - using System.Runtime.InteropServices;
Read this http://msdn.microsoft.com/en-us/libr...ya(VS.80).aspx
C# Syntax (Toggle Plain Text)
xyz p = new xyz(); MessageBox.Show(Marshal.SizeOf(p).ToString());
Read this http://msdn.microsoft.com/en-us/libr...ya(VS.80).aspx
Last edited by adatapost; Jun 26th, 2009 at 9:08 am.
Failure is not fatal, but failure to change might be. - John Wooden
In .NET the char data type is is 16bit, most other languages it is 8bit. You will also need to watch out for that.
Because of this unmanaged attribute a size of char is 8 bits.
So the size of struct is 14 bytes.
C# Syntax (Toggle Plain Text)
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Ansi, Size = 14)] struct xyz { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public char[] localc; short localshort; } ... ... xyz p = new xyz(); int size=Marshal.SizeOf(p); ..
So the size of struct is 14 bytes.
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- IFrame without fixed size (Site Layout and Usability)
- Array with Variable Size in a Structure (C++)
- fstream Tutorial (C++)
- New font size (DaniWeb Community Feedback)
- Java III help needed to be done !!! (Java)
- Tkinter Window Fixed Size (Python)
- C--Structures(inside structure declaration of variable of same type is not allowed) (C)
- Need help on order queuing program (C++)
- Amibguity in C & C++ (C++)
Other Threads in the C# Forum
- Previous Thread: csv read by jet engine throws error 'specified cast is not valid'
- Next Thread: C#.net
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox connection console control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment developer development draganddrop drawing editing encryption enum event excel file form format forms function gdi+ hospitalmanagementinformationsystem httpwebrequest image imageprocessing index input install java label list listbox mandelbrot math mouseclick mysql operator oracle path photoshop picturebox pixelinversion post priviallages. programming radians regex remote remoting richtextbox rows serialization server sleep socket sql statistics stream string table temperature text textbox thread time timer txt update uploadatextfile usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf xml






