By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Marshaling array of struct containing array

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.