473,372 Members | 1,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 3608
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
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
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
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
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
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
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.