473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Filling Structs.

Hello

I want to write a C# (or VB.NET) program which reads the structure of
a PE (odinary win32 executable) file. These files have a long header
(512 bytes or so). The definition of the header exists in WINNT.h file
(you can see many "struct"s there).

Here comes my problem: how can I read a file and "fill" the structs?
Note that a method such as FileStream.Read can only fill an array not
a struct:

public override int Read(

in byte[] array,

int offset,

int count

);

The conversion from an array to those structs is really cumbersome. In
an unmanaged language, I could use pointers to have the array and the
struct share the same memory, thus filling the struct members with
array's values. But, in this managed context, that's not the case.

Any help is appreciated.

PS: If possible, I want to write a program without "unsafe" blocks or
Windows API functions.

thnx in advance,

MSD.
Nov 16 '05 #1
3 3651
Here comes my problem: how can I read a file and "fill" the structs?


With a BinaryReader you can fill the struct member by member.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<Oe**************@TK2MSFTNGP15.phx.gbl>...
Here comes my problem: how can I read a file and "fill" the structs?


With a BinaryReader you can fill the struct member by member.

Mattias


Hi Mattias, thnx 4 answering.
That was the thing I once thought about, but there's a big problem to
this solution. If I'd wanted to fill one struct many times, I should
have written a BinaryReader. But I have to fill many structs just
once, so it's not worth writing a BinaryReader for each one.

Another solution I may come across, is using
[StructLayout(LayoutKind.Sequential, Pack = 0)]
attribute for my struct. I think because the fields place in memory
sequentially, there must be some way to access them in order.

However, thnx again.
It'll great of u if u answer me again.
& sorry for gramatical errors.

MSD.
Nov 16 '05 #3
If you redefine the struct in your code, you may do something like this.

using System.Runtime.InteropServices;

Stream input = new FileStream(...);
byte [] buff = new byte [Marshal.SizeOf(HEADER)];
input.Read(buff, 0x0, buff.Length);
IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
Marshal.Copy(buff, 0x0, ptr, buff.Length);
HEADER header = (HEADER) Marshal.PtrToStructure(ptr, typeof(HEADER));
Marshal.FreeHGlobal(ptr);
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
struct HEADER
{
....
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"MSDousti" <MS******@myrealbox.com> wrote in message
news:bf**************************@posting.google.c om...
Hello

I want to write a C# (or VB.NET) program which reads the structure of
a PE (odinary win32 executable) file. These files have a long header
(512 bytes or so). The definition of the header exists in WINNT.h file
(you can see many "struct"s there).

Here comes my problem: how can I read a file and "fill" the structs?
Note that a method such as FileStream.Read can only fill an array not
a struct:

public override int Read(

in byte[] array,

int offset,

int count

);

The conversion from an array to those structs is really cumbersome. In
an unmanaged language, I could use pointers to have the array and the
struct share the same memory, thus filling the struct members with
array's values. But, in this managed context, that's not the case.

Any help is appreciated.

PS: If possible, I want to write a program without "unsafe" blocks or
Windows API functions.

thnx in advance,

MSD.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.