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

C++/CLI array in struct

I recently had to use someone's struct from a native app to receive data over
Udp. The struct has a array member which looked like this:

struct sensorHdr{
char sName[64];
};

When I tried to make this a managed struct by adding either the value struct
or ref struct I received a compile error stating that mixed types are not
supported. I understand this, but I don't know how to create a char array in
a C++/CLI struct using the array<Byte>(64) format.

Thanks
Nick
Nov 17 '05 #1
3 11426
GrkEngineer wrote:
I recently had to use someone's struct from a native app to receive data over
Udp. The struct has a array member which looked like this:

struct sensorHdr{
char sName[64];
};

When I tried to make this a managed struct by adding either the value struct
or ref struct I received a compile error stating that mixed types are not
supported. I understand this, but I don't know how to create a char array in
a C++/CLI struct using the array<Byte>(64) format.

Thanks
Nick


Yes, array<Byte>(64) looks good to me, and this code compiles and seems
to work fine:

ref class sensorHdr
{
public:
cli::array<char> ^ sName;
sensorHdr(): sName( gcnew cli::array<char>(64) ) {}
};

You just have to pin the pointer before you pass it to a native API call:

cli::pin_ptr<char> s( & sName[0] );
CallCFunction(s);

Pinning means the .NET framework locks the location of the array in the
memory, preventing any possible relocation while your native code is
working on it.

Give it a try. Here's an alternative solution:

ref class sensorHdr
{
public:
char* sName;
sensorHdr() : sName(new char[64]) { }
~sensorHdr() { delete [] sName; }
};

This one compiles too, however, when this class is used from C#, for
example, you must call Dispose on it, to prevent unmanaged memory leak.
You may want to add a finalizer too. I'd go with the array<char>.

Tom
Nov 17 '05 #2
Tamas,

Thanks for the help. I just have a couple questions more. I noticed you used
ref class instead of ref struct. Will this still work as a struct or is there
no difference because you declared the members public? Can I pass a pinned
ref class over network or shared memory and have the native program receive
it as a struct? Also, will the sizeof(sensorHdr) give me the correct size. I
know using the second approach with the pointer will not. But will using the
cli::array?

Nick

"Tamas Demjen" wrote:
GrkEngineer wrote:
I recently had to use someone's struct from a native app to receive data over
Udp. The struct has a array member which looked like this:

struct sensorHdr{
char sName[64];
};

When I tried to make this a managed struct by adding either the value struct
or ref struct I received a compile error stating that mixed types are not
supported. I understand this, but I don't know how to create a char array in
a C++/CLI struct using the array<Byte>(64) format.

Thanks
Nick


Yes, array<Byte>(64) looks good to me, and this code compiles and seems
to work fine:

ref class sensorHdr
{
public:
cli::array<char> ^ sName;
sensorHdr(): sName( gcnew cli::array<char>(64) ) {}
};

You just have to pin the pointer before you pass it to a native API call:

cli::pin_ptr<char> s( & sName[0] );
CallCFunction(s);

Pinning means the .NET framework locks the location of the array in the
memory, preventing any possible relocation while your native code is
working on it.

Give it a try. Here's an alternative solution:

ref class sensorHdr
{
public:
char* sName;
sensorHdr() : sName(new char[64]) { }
~sensorHdr() { delete [] sName; }
};

This one compiles too, however, when this class is used from C#, for
example, you must call Dispose on it, to prevent unmanaged memory leak.
You may want to add a finalizer too. I'd go with the array<char>.

Tom

Nov 17 '05 #3
Nick,

I think ref struct is almost the same as ref class, the only difference
is whether members are public or private by default.

No, I don't think you should pass a ref class to unmanaged code.
Although it may seem your class is not inherited from anything and it
only declares one member, it actually is inherited from System::Object,
the parent of all .NET classes, and it most likely has implicit members
that you don't know of. Most importantly, you don't know the memory
layout, and you don't know what's inside the array container (I
guarantee you it's not a raw storage, it has further members, such as
the length of the array). You should only pin the handle to the array's
first item:

cli::pin_ptr<char> s( & sName[0] );

sizeof(sensorHrd) is most likely not 64. Don't rely on that. Instead,
use sName->Length to get the number of items in the array (which is the
size of the array in bytes in your case).

I think a .NET array is guaranteed to be contiguous in the memory, at
least this link claims it is:
http://msdn.microsoft.com/library/de...ures_guide.asp

Most containers aren't, but array is, as long as it stores value types
(such as char).

BTW, you have two more options: System::String, and memory stream. They
both allocate contiguous memory. I'd still use array<char>, which is a
wrapper around System::Array. Would use memory stream if I wanted to
append data dynamically to it.

Tom
GrkEngineer wrote:
Tamas,

Thanks for the help. I just have a couple questions more. I noticed you used
ref class instead of ref struct. Will this still work as a struct or is there
no difference because you declared the members public? Can I pass a pinned
ref class over network or shared memory and have the native program receive
it as a struct? Also, will the sizeof(sensorHdr) give me the correct size. I
know using the second approach with the pointer will not. But will using the
cli::array?

Nick

Nov 17 '05 #4

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

Similar topics

1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
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: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
20
by: Cyn | last post by:
Hi, I want to create a general array structure which can hold all types. Something like this: struct ARRAY { void **array; size_t size; };
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...

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.