473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4304
Hi Paul,

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

[StructLayout(La youtKind.Sequen tial, 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.AllocHG lobal(size);
GCHandle gch = GCHandle.Alloc( ip, GCHandleType.Pi nned);

Marshal.Copy(da ta, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marsha l.PtrToStructur e(ip, t);

gch.Free();
Marshal.FreeHGl obal(ip);

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

"Pawe³" <no*****@nowher e.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(La youtKind.Sequen tial, Pack=1)]
public struct REKORD1
{
byte b1;
ushort w1;
ushort w2;
uint dw1;
}

byte[] data; // read from file
GCHandle gData = GCHandle.Alloc( data, GCHandleType.Pi nned);
REKORD1 newRecord =
(REKORD1)Marsha l.PtrToStructur e(gData.AddrOfP innendObject(),
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:opsd2kk9h3 klbvpo@stone...
Hi Paul,

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

[StructLayout(La youtKind.Sequen tial, 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.AllocHG lobal(size);
//GCHandle gch = GCHandle.Alloc( ip, GCHandleType.Pi nned);

Marshal.Copy(da ta, 0, ip, size);
REKORD1 newRecord = (REKORD1)Marsha l.PtrToStructur e(ip, t);

//gch.Free();
Marshal.FreeHGl obal(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2404
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
3
2626
by: poifull | last post by:
Hi All, What is the proper way to read a binary file into a byte? I am using BinaryReader to read from a Stream and call the ReadByte method of the BinaryReader object. The method I'm using leads to the second question. I got the "Conversion buffer overflow" error when I run the following code: Stream s = openFileDialog1.OpenFile();
10
2745
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
8
28470
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
40471
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am familiar with "typedef int NUMBER", but how does it work with structures. Tilak
15
2682
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it then adds values to "jd" 3.) in the end it prints values of "jd".
20
1859
by: Francine.Neary | last post by:
People seem to have different views as to where the C reserved word "struct" comes from. One explanation is that it is a shortening of "structure", and another is that it is an acronym for "single type representing useful compound types". Does anyone here know the history of the word?
8
2171
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address (where there is an
4
10624
by: MetaKith | last post by:
Hi, Please have a look on the next code : struct foo{ float b; char z; }; struct foo foo_init = {234.77,"xcdy"}; struct foo *prt_prt=&foo_init;
0
9454
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
10102
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
10038
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
8933
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
7460
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
6712
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
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.