473,385 Members | 1,907 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,385 software developers and data experts.

Structure array inside structure when passing reference to C++

I have a struct in C#-project and C++-project. I use the struct in C# but it
has to be filled in C++.

I know that arrays work like this:

In C++ code:

struct teststruct {
char str[100];
float myvar[2];
};

CPPPROJECTDLLDECL int DLLCALL callteststruct(teststruct *data) {
strcpy(data->str, "hello");
data->myvar[0] = 123.45f;
data->myvar[1] = 543.21f;
return 0;
}

In C# code:

using System.Runtime.InteropServices;

[Serializable()]
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct teststruct {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
public string str;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public float[] myvar;
};

[DllImport("CppProject.dll")]
static public extern unsafe int callteststruct(ref teststruct data);

private void Page_Load(object sender, System.EventArgs e)
{
teststruct data = new teststruct();
callteststruct(ref data);
}
Now I need to add a structure array inside the "teststruct". It should look
something like this in C++:

struct teststruct2 {
int intvalue;
int anotherintvalue
char text[100];
}

struct teststruct {
char str[100];
float myvar[2];
teststruct2 teststruct2array[100];
};

But how to do it in C#?
I really would appreciate some help.
Thanks.

Mikko
Nov 15 '05 #1
1 6397
Mikko,

You would do it the same way. First, declare the teststruct2 structure,
like so:

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
struct teststruct2
{
int intvalue;
int anotherintvalue
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
char text[100];
}

Then alter your definition of teststruct like so:

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct teststruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
public string str;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public float[] myvar;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=100)]
public teststruct2 teststruct2array[];
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mikko Penkkimäki" <mp******@hotmail.com> wrote in message
news:3f**********************@news.songnet.fi...
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++.

I know that arrays work like this:

In C++ code:

struct teststruct {
char str[100];
float myvar[2];
};

CPPPROJECTDLLDECL int DLLCALL callteststruct(teststruct *data) {
strcpy(data->str, "hello");
data->myvar[0] = 123.45f;
data->myvar[1] = 543.21f;
return 0;
}

In C# code:

using System.Runtime.InteropServices;

[Serializable()]
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct teststruct {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
public string str;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public float[] myvar;
};

[DllImport("CppProject.dll")]
static public extern unsafe int callteststruct(ref teststruct data);

private void Page_Load(object sender, System.EventArgs e)
{
teststruct data = new teststruct();
callteststruct(ref data);
}
Now I need to add a structure array inside the "teststruct". It should look something like this in C++:

struct teststruct2 {
int intvalue;
int anotherintvalue
char text[100];
}

struct teststruct {
char str[100];
float myvar[2];
teststruct2 teststruct2array[100];
};

But how to do it in C#?
I really would appreciate some help.
Thanks.

Mikko

Nov 15 '05 #2

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: Panda2 | last post by:
Say I have a structure such as: struct Barn{ char type; int number; }animal so for example we might have data like animal.type cow animal.number 10
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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,...
0
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...
0
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
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...

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.