472,110 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

reading "binary struct"

Hi!
I want to read with C# some binary file with well defined binary structure.
In C++ I was declaring appropriate struct, like:

typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1;
REKORD1 buff ;

and then read record from file with
read (file, (void *) &buff, sizeof (REKORD1));

Is this technique possible with C#? Now I have to "manually" bond together
particular values with ReadByte().

Thanks for any help
Paul
Nov 16 '05 #1
3 4181
Hi Paul,

It is possible with C# to load a byte array into a struct with something like

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct REKORD1
{
public byte b1;
public ushort w1;
public ushort w2;
public uint dw1;
}

....

byte[] data = GetData(); // read from file or something
int size = Marshal.SizeOf(typeof(REKORD1);

IntPtr ip = Marshal.AllocHGlobal(size);
GCHandle gch = GCHandle.Alloc(ip, GCHandleType.Pinned);

Marshal.Copy(data, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marshal.PtrToStructure(ip, t);

gch.Free();
Marshal.FreeHGlobal(ip);

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi,
inline

"Pawe³" <no*****@nowhere.com> wrote in message
news:41********@news.home.net.pl...
Hi!
I want to read with C# some binary file with well defined binary
structure.
In C++ I was declaring appropriate struct, like:

typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1;
REKORD1 buff ;

and then read record from file with
read (file, (void *) &buff, sizeof (REKORD1));

Is this technique possible with C#? Now I have to "manually" bond together
particular values with ReadByte().
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct REKORD1
{
byte b1;
ushort w1;
ushort w2;
uint dw1;
}

byte[] data; // read from file
GCHandle gData = GCHandle.Alloc(data, GCHandleType.Pinned);
REKORD1 newRecord =
(REKORD1)Marshal.PtrToStructure(gData.AddrOfPinnen dObject(),
typeof(REKORD1));
gch.Free();
HTH,
greetings


Thanks for any help
Paul

Nov 16 '05 #3
Hi

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsd2kk9h3klbvpo@stone...
Hi Paul,

It is possible with C# to load a byte array into a struct with something
like

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct REKORD1
{
public byte b1;
public ushort w1;
public ushort w2;
public uint dw1;
}

...

byte[] data = GetData(); // read from file or something
int size = Marshal.SizeOf(typeof(REKORD1);

IntPtr ip = Marshal.AllocHGlobal(size);
//GCHandle gch = GCHandle.Alloc(ip, GCHandleType.Pinned);

Marshal.Copy(data, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marshal.PtrToStructure(ip, t);

//gch.Free();
Marshal.FreeHGlobal(ip);
You allocate memory on the heap and then you pin it ? Memory obtained from
the unmanaged heap (AllocHGlobal) does not need pinning.
If you pin the byte array you can get a pointer to it and then use that for
PtrToStructure().
greetings


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Pantokrator | last post: by
8 posts views Thread by Mohammad Omer Nasir | last post: by
8 posts views Thread by cman | last post: by
15 posts views Thread by arnuld | last post: by
20 posts views Thread by Francine.Neary | last post: by
8 posts views Thread by anon.asdf | last post: by
reply views Thread by leo001 | last post: by

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.