473,405 Members | 2,445 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,405 software developers and data experts.

Casting managed array

I have a structure that looks like this:

[StructLayout(LayoutKind::Sequential)]
public __value struct mytype
{
float first;
float second;
};

I am trying to pass arrays of these things over a socket. So I have:

mytype items __gc []

and need to provide

Bytes rawbytes __gc []

to the socket code.

Right now I am using BitConvertor to do this conversion. It works for the
moment but I don't consider this acceptable. Partly because of the obvious
inefficiency but mostly because I want to use the same socket code to deal
with simple arrays of managed floats. I've tried many ways of casting the
array and it's just not happening.

Type saftey isn't a big concern for me at this level because the code is
private to my assembly. (The same assembly contains implementations for
both sides of the socket.) I wanted to use dotnet rather than win32
sockets because my client is written entirely in managed code.

Can anyone help me with a trick for those arrays?
Jul 21 '05 #1
4 1448
Stewart,

For an array of float you should be able to use Buffer::BlockCopy to
copy the bits to a byte array.

For an array of your mytype struct, you would have to pin the array
and memcpy the content to a byte array (that's also pinned).

There's no way to cast between the different array types, so you'll
always have to duplicate the data.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
You should look at the Marshal class; It contains copying functions that
that can do these kinds of conversions. Also, binary serialization might be
interesting to you.

Niki

"Stewart Stevens" <fi*******@inaplacecalledadi.co.kiwi> wrote in
news:10***************@kyle.snap.net.nz...
I have a structure that looks like this:

[StructLayout(LayoutKind::Sequential)]
public __value struct mytype
{
float first;
float second;
};

I am trying to pass arrays of these things over a socket. So I have:

mytype items __gc []

and need to provide

Bytes rawbytes __gc []

to the socket code.

Right now I am using BitConvertor to do this conversion. It works for the
moment but I don't consider this acceptable. Partly because of the obvious
inefficiency but mostly because I want to use the same socket code to deal
with simple arrays of managed floats. I've tried many ways of casting the
array and it's just not happening.

Type saftey isn't a big concern for me at this level because the code is
private to my assembly. (The same assembly contains implementations for
both sides of the socket.) I wanted to use dotnet rather than win32
sockets because my client is written entirely in managed code.

Can anyone help me with a trick for those arrays?

Jul 21 '05 #3
Thanks very much....that makes things a lot tidier.
I was a bit hung up on avoiding the copy....

Mattias Sjögren wrote:
Stewart,

For an array of float you should be able to use Buffer::BlockCopy to
copy the bits to a byte array.

For an array of your mytype struct, you would have to pin the array
and memcpy the content to a byte array (that's also pinned).

There's no way to cast between the different array types, so you'll
always have to duplicate the data.

Mattias


Jul 21 '05 #4
I am new to dotnet ... but I got the impression that the Marshal class was
used for moving data between managed and unmanaged code ... this situation
is all managed so the Buffer class that Mattias mentioned seems more
appropriate.

The binary serialization is actually exactly what I want to avoid. Most of
my interface exploits the safety and conveniance of the Remoting
architecutre using binary serialization but I have to move a lot of data
quickly and the binary serialization is totally inappropriate for that. Not
only is the serialization time consuming but there is a serious size
overhead....400 bytes worth of my simple float structures are serialized to
1051 bytes!

My first stab at the socket implementation - with the inefficiencies I was
complaining about in my post (+ some other problems I am fixing!) gave me
up to 10 fold speed increase.

Cheers
Stewart

Niki Estner wrote:
You should look at the Marshal class; It contains copying functions that
that can do these kinds of conversions. Also, binary serialization might
be interesting to you.

Niki

Jul 21 '05 #5

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

Similar topics

4
by: c | last post by:
Hello All, I am struggling with simple casting issue within c# and would really appreciate some insight into where I am going wrong. I have two classes, ClassA and ClassB. Each implements a...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
4
by: kelli | last post by:
i am new to c# so if this is a trivial problem, forgive me! i've searched the web and after 2 days still cannot solve it. i am converting a c++ application to c# - the problem code is listed...
6
by: Joe Thompson | last post by:
Using VS.Net 2003 - a managed VC++ WinForms app: If I have an unmanged structure like typedef struct { unsigned char Field1; unsigned char Field2; unsigned int Field3: 6; unsigned int...
4
by: Stewart Stevens | last post by:
I have a structure that looks like this: public __value struct mytype { float first; float second; }; I am trying to pass arrays of these things over a socket. So I have:
8
by: tom | last post by:
Hi All, I'm stuck whit an issue I can't seem to resolve in C#: I have an arry of bytes which I would like to "recast" to an array of structs with an Explicit layout. I tried the...
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
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
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,...
0
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...

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.