473,511 Members | 13,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshaling array of struct containing array

Hello,

Does anyone know how to create a struct that will marshal to the
following C++ struct A, containing an array of the user defined String10
type:

struct String10
{
char SLen;
char S[10];
}

struct A
{
int A1;
String10 A2[20];
}

I tried:

[StructLayout(LayoutKind.Sequential)]
struct String10
{
byte SLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
byte[] S;
}

[StructLayout(LayoutKind.Sequential)]
struct A
{
int A1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
String10[] A2;
}

and

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

But that won't marshal:

"Type A can not be marshaled as an unmanaged structure; no meaningful
size or offset can be computed."

I also tried

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct String10
{
byte SLen;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
string S;
}

But the problem is in the A struct, which contains an array of String10.
How I exaclty define String10 does not make a big difference. Getting
Marshal.Sizeof(String10) is not a problem.

Anyone?
--
Rudy Velthuis

"A mathematician is a device for turning coffee into theorems."
- Paul Erdos
Nov 15 '05 #1
3 3626
At 23:26:27, 06.03.2004, Rudy Velthuis wrote:
But the problem is in the A struct, which contains an array of String10.
How I exaclty define String10 does not make a big difference. Getting
Marshal.Sizeof(String10) is not a problem.

Anyone?


Hmmm... would a custom mashaler be the solution? Or is that not necessary?
--
Rudy Velthuis

"There are some experiences in life which should not be demanded twice
from any man, and one of them is listening to the Brahms Requiem."
- George Bernard Shaw (1856-1950)
Nov 15 '05 #2
I had a similar problem with arrays of doubles. This is my implemetation
which works. I think the initialise method is necessary and should be
called after creating the structure.

I hope this is what you are looking for.

John Prior

[StructLayout(LayoutKind.Sequential)]
public struct DividendArray
{
public int NoDividends;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst=11 )]
public double[] DividendAmounts;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst=11 )]
public double[] DividendTimes;

public void Initialize()
{
DividendAmounts = new double[11];
DividendTimes = new double[11];
}
}
"Rudy Velthuis" <rv*******@gmx.de> wrote in message
news:xn**********************@news.microsoft.com.. .
Hello,

Does anyone know how to create a struct that will marshal to the
following C++ struct A, containing an array of the user defined String10
type:

struct String10
{
char SLen;
char S[10];
}

struct A
{
int A1;
String10 A2[20];
}

I tried:

[StructLayout(LayoutKind.Sequential)]
struct String10
{
byte SLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
byte[] S;
}

[StructLayout(LayoutKind.Sequential)]
struct A
{
int A1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
String10[] A2;
}

and

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

But that won't marshal:

"Type A can not be marshaled as an unmanaged structure; no meaningful
size or offset can be computed."

I also tried

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct String10
{
byte SLen;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
string S;
}

But the problem is in the A struct, which contains an array of String10.
How I exaclty define String10 does not make a big difference. Getting
Marshal.Sizeof(String10) is not a problem.

Anyone?
--
Rudy Velthuis

"A mathematician is a device for turning coffee into theorems."
- Paul Erdos

Nov 15 '05 #3
At 02:45:37, 07.03.2004, John Prior wrote:
I had a similar problem with arrays of doubles. This is my
implemetation which works. I think the initialise method is necessary
and should be called after creating the structure.

I hope this is what you are looking for.

John Prior

[StructLayout(LayoutKind.Sequential)]
public struct DividendArray
{
public int NoDividends;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst=11 )]
public double[] DividendAmounts;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst=11 )]
public double[] DividendTimes;

public void Initialize()
{
DividendAmounts = new double[11];
DividendTimes = new double[11];
}
}


Not exactly. I had no problem with marshaling the String10 type, which
contained an array of byte. The problem was marshaling an array of
String10, i.e. an array of a type to be marshaled. I think the problem is
the recursive, or nested, marshaling necessary.
--
Rudy Velthuis

"I am become death, shatterer of worlds."
- Robert J. Oppenheimer (1904-1967) (citing from the Bhagavad Gita,
after witnessing the world's first nuclear explosion)
Nov 15 '05 #4

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

Similar topics

3
2642
by: Nikolay Petrov | last post by:
Guys, please help. I am trying to make this work from at least 4 months. I am new to programming and some things are difficult to me, but I really need to make my project work. I can't find...
6
4176
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
10
6665
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
5
96473
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain...
0
1015
by: Tom Kent | last post by:
I have a dll that is expecting a 3x3 array which I'm am using DllImport to access. In addition, this array is located in a struct that is getting passed. I'm having problems that I think are...
2
8620
by: Thomas Kent | last post by:
I have a dll that is expecting a 3x3 array which I'm am using DllImport to access. In addition, this array is located in a struct that is getting passed. I'm having problems that I think are...
0
1457
by: Jeff | last post by:
Hi guys Mattias, thanx for answering my last question Well, I'm struggling with marshaling a struct that has **ptr to an array of arrays of struct. Why? I'm stuck with it 1. I need to know...
2
1859
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I...
2
3400
by: kevou | last post by:
hi, I'm currently trying to deal with this struct from C lib using Platform invoke in C#. Here is the C struct: typedef struct { short myshort;
0
7356
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,...
0
7427
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...
1
7085
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...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5069
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...
0
3227
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
0
449
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...

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.