By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

reading "binary struct"

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.