Marshaling array of struct containing array

R

Rudy Velthuis

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?
 
R

Rudy Velthuis

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)
 
J

John Prior

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];
}
}
 
R

Rudy Velthuis

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top