473,769 Members | 2,143 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(La youtKind.Sequen tial)]
struct String10
{
byte SLen;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 10)]
byte[] S;
}

[StructLayout(La youtKind.Sequen tial)]
struct A
{
int A1;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20)]
String10[] A2;
}

and

Console.WriteLi ne(Marshal.Size Of(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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct String10
{
byte SLen;
[MarshalAs(Unman agedType.ByValT Str, 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 3644
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(La youtKind.Sequen tial)]
public struct DividendArray
{
public int NoDividends;

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

[ MarshalAs( UnmanagedType.B yValArray, 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(La youtKind.Sequen tial)]
struct String10
{
byte SLen;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 10)]
byte[] S;
}

[StructLayout(La youtKind.Sequen tial)]
struct A
{
int A1;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 20)]
String10[] A2;
}

and

Console.WriteLi ne(Marshal.Size Of(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(La youtKind.Sequen tial, CharSet = CharSet.Ansi)]
struct String10
{
byte SLen;
[MarshalAs(Unman agedType.ByValT Str, 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(La youtKind.Sequen tial)]
public struct DividendArray
{
public int NoDividends;

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

[ MarshalAs( UnmanagedType.B yValArray, 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
2668
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 anything helpfull in internet for 4 months ;-( . Please someone who is more expirenced help me. I am trying to use the Terminal Server APIs There is as an API function WTSEnumerateSessions, which returns a WTS_SESSION_INFO custom structure n times....
6
4206
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 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
10
6693
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
96496
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 what it is. Is converting a C-style struct to a C# class (or struct) marshalling? And what about the function exports? Are those Marshalling too? Thanks for your help and thanks for helping me with the Dll exports (the several messages I posted in...
0
1026
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 located somewhere in how I'm marshaling it. Any input would be appreciated. (examples are shortened for clarity, let me know if something more complete would be useful) /*C code*/ typedef float RotMtxType; typedef struct { double ccNum; RotMtxType...
2
8696
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 located somewhere in how I'm marshaling it. Any input would be appreciated. (examples are shortened for clarity, let me know if something more complete would be useful /*C code* typedef float RotMtxType typedef struct double ccNum RotMtxType...
0
1472
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 how to get to the contents of "dumberstruct" contained within "dumbstruct" (i.e. dumbstruct.dstruct) 2. How to get to dumbstruct.tag_field_value and dumbstruct.field_value Code follows
2
1874
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 can't find the correct sequence to declare the marshal attributes of my structures, I always got an exception at runtime when marshaling my data. Here an example of code, which gives me the exception "Type TypeData can not be marshaled as an...
2
3419
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
9579
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10208
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.