473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting managed array

I have a structure that looks like this:

[StructLayout(La youtKind::Seque ntial)]
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 1497
Stewart,

For an array of float you should be able to use Buffer::BlockCo py 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*******@inap lacecalledadi.c o.kiwi> wrote in
news:10******** *******@kyle.sn ap.net.nz...
I have a structure that looks like this:

[StructLayout(La youtKind::Seque ntial)]
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::BlockCo py 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
1699
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 "public static implicit operator" to allow silent casting from one to the other. This works fine, per the documents I have read. However, I am unable to do this:
3
1681
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
2564
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 for all business entities) dictates that the implementing class expose a Dirty property (for state). The base class (that actually manages state, once for all inheritors)
7
3677
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; but it complains for the following lines
16
14134
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 byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
4
4506
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 below. the c++ code uses a byte array (msg) to hold a serial message. since the message can be of different layouts, the array is cast to the appropriate layout "type" (struct SERIAL_CAN_MSG_TYPE, in this example). to complicate things, there is an...
6
3392
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 Field4: 2;
4
349
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
5090
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 Buffer.BlockCopy method, but that one complains my struct is not a primitive type. Any Suggestions ?
0
8830
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
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
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
9247
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
8243
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
6796
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
6074
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();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
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.