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 / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

structs, pointers and other such magicks...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MZK - 16 Apr 2004 22:56 GMT
Having asked a question recently about casting a byte buffer to a struct and receiving an excellent response from Mattias Sjorgen (sorry about that o ;) which resulted in the following code

struct rec01
 ushort rt
 ..

byte[] buffer = new byte[512]
rec01 rec

// fill buffer with dat

unsafe
 fixed (byte *p = buffer) {
   rec = *((rec01*)p)
 

Now this works, but I am now stuck on something else...  The structures I want to replicate in c# from c++ are of the type

struct rec01
 __int16 rt
 char id[10]
 char name[32]
 ..

I've tried using the following for the id field
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=10)
string id;

and
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)
byte[] id;

but both yield the same compile error on the above buffer/struct cast !

Does anybody have any ideas or is this type of thing not possible in c#

Thanks in advance
Mattias Sj?gren - 17 Apr 2004 03:06 GMT
>from Mattias Sjorgen (sorry about that o ;)

Close enough :)

>Does anybody have any ideas or is this type of thing not possible in c# ?

No, unsafe code only works on value types without any reference type
members, which unfortunately excludes arrays and strings.

So the other option is probably to work with the Marshal class
instead. Here are some egeneric routines for copying a struct to/from
a byte array

http://groups.google.com/groups?selm=%237tARJk5BHA.1656%40tkmsftngp05

FWIW, C# v2 will have a solution to this, letting you write

unsafe struct rec01 {
 public short rt;
 public fixed byte id[10];
 public fixed byte name[32];
 ...

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







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.