Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2004

Tip: Looking for answers? Try searching our database.

Byte Array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fireangel - 07 Oct 2004 13:13 GMT
I want to cast a class into a byte array.  I've seen some examples of this
floating around, but they all have simple data members.  What happens if I
cast something that has a ArrayList or an Array??  Does it loop through the
array and cast them all and I end up with a big array (This is what I hope
will happen)?  Or does it simple cast the pointer address and I end up with a
small array??  

Related question:  How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast??  And if I
try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..

GE
Tomas Restrepo \(MVP\) - 08 Oct 2004 00:41 GMT
> I want to cast a class into a byte array.  I've seen some examples of this
> floating around, but they all have simple data members.  What happens if I
[quoted text clipped - 9 lines]
> I haven't started in on this project yet, so I can't say as I've tried any
> of this..

Are we talking about managed ref types here? If so, you should *never* do
this, you're won't get what you expect, and you're likely to end up
corrupting the managed heap if you manipulate it this way.

Why do you need to do this? If you're trying to call into native code
passing this, then there are appropriate methods for this (P/Invoke,
StructLayout, etc).

If you're trying, for example, to save the contents of the class to disk or
something like that, then using Serialization and the Serialization
formatters (e.g. BinaryFormatter) are the way to go.

Signature

Tomas Restrepo
tomasr@mvps.org

Fireangel - 09 Oct 2004 01:41 GMT
I'm using only managed types.

In a nut shell, I'm trying to send whole classes across the network using
the socket class.  The socket class only sends and received using Byte Arrays
(__gc array).  I *THINK* i've seen some examples of ppl casting a class into
a byte array, sending it, then casting it back into the class on the other
end.  I say I think because I was looking for something else when I came
across it, and it was a fair amount ago.  

I have to cast into a byte array.  I have no choice.  I do want to make sure
i get back what I want though.  And If I can avoid having to write a whole
bunch of functions to create a byte array, I would be very happy :)

GE

"Tomas Restrepo (MVP)" wrote:

> > I want to cast a class into a byte array.  I've seen some examples of this
> > floating around, but they all have simple data members.  What happens if I
[quoted text clipped - 24 lines]
> something like that, then using Serialization and the Serialization
> formatters (e.g. BinaryFormatter) are the way to go.
Tomas Restrepo \(MVP\) - 09 Oct 2004 03:59 GMT
Fireangel,

> I'm using only managed types.
>
[quoted text clipped - 4 lines]
> end.  I say I think because I was looking for something else when I came
> across it, and it was a fair amount ago.

That's common unmanaged C++ practice, but won't really do the trick in
managed code.

> I have to cast into a byte array.  I have no choice.  I do want to make sure
> i get back what I want though.  And If I can avoid having to write a whole
> bunch of functions to create a byte array, I would be very happy :)

Serialization is your friend. Basically, all you'd need to do is mark your
managed types as [ Serializable ] and use the BinaryFormatter class to
serialize it into a stream and deserialize them from a string.

There are plenty of examples of this on the net...

That said, since it seems you have control of both client and server, why
invent your own protocol? Just use .NET remoting and be done with it....

Signature

Tomas Restrepo
tomasr@mvps.org


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.