473,516 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the size when a struct array in aother struct ?

two struct in c#

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class A
{
int i1;
byte i2;
}

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class B
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=25 )]
A[] a;
}

then i call Marshal.SizeOf(typeof(B)) will exception and talk me
{"Type B can not be marshaled as an unmanaged structure; no meaningful
size or offset can be computed." }

How to get the size when a struct array in aother struct ?
Thanks
Nov 16 '05 #1
1 1619
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public class B
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst=25 )]
A[] a;
}


ByVal arrays of structs isn't supported by the marshaler, you will not
be able to use such a struct in interop calls. So getting it's size if
probably not meaningful either. But you can still do

25 * Marshal.SizeOf(typeof(A))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11938
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int SizeOfArray(int a) { return (sizeof(a)/sizeof(a)); } main() { int a={1,2,3}; printf("%d\n",SizeOfArray(a));
4
6389
by: Angus Comber | last post by:
Hello If I do this: struct mystruct { long nKey; char szItem; };
25
2661
by: prashna | last post by:
Hi all, I have seen a piece of code(while doing code review) which declared an array of size 0.One of my friend told although it is not standard C,some compilers will support this..I am very curious to know the use of it.. The code was compiled using Diab C compiler. Also the array was declared in structure like this typedef struct...
22
2427
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now...
7
2174
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be the size of "ethernet_frame" struct and why ? typedef struct { //This struct defines Arp--Request.
6
2287
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why? The compiler i'm using is Dev c++. #include <stdio.h> #include <stdlib.h>
6
4987
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public class MyClass {
8
10145
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
1
2746
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define the fixed size using In the attached console application example when I try to address the fields in the class I get an execption.
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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
7581
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
5110
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
4773
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
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.