Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Copying into structure

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Sun Aug 23, 2009 12:09 am    Post subject: Copying into structure Reply with quote

I'm trying to copy a chunk of memory into a structure...

actually its a PE header, that i've loaded with globalalloc and readfile,

its all a random jumble of bytes

ex.

00 09 93 05 98 ... and so on

I want to copy it into a structure (which I know how the size and order goes)

do i use memcpy? I used it and I get weird results, like the first 2 bytes: MZ, i do something simple in a console to just see what i'm working with and get errors.

also, how are the endians arranged? I know they're backwards, or its automatically done for me?
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 5:07 am    Post subject: Reply with quote

yes you could use memcpy

can you be a bit more clear about what is going wrong ? maybe post some code or something

x86 system uses little endian which means most significant byte is rightmost, and least significant leftmost
Back to top
View user's profile Send private message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Sun Aug 23, 2009 6:23 am    Post subject: Reply with quote

Thanks, I meant lets take a look at the first 64 bytes, the dos header, even if I wasn't going to use it, I want to import it, so I've allocated a chunk of memory like so

file -> globalalloc -> readfile -> (void *)mem

so I initiate a struct like..

struct PE{
WORD magic
...};

struct PE dos_header;

....
....
memcpy(&dos_header,mem,64);

so after I do that, I can access dos_header like a regular structure?

dos_header.magic = 'MZ'

and so on?
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sun Aug 23, 2009 6:28 am    Post subject: Reply with quote

Yes.

but there's no reason to set the "magic" property of the struct again, after you already copied the memory lol
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sun Aug 23, 2009 7:34 am    Post subject: Reply with quote

I think you should read up on the PE File Format a bit more. Iczelion has some excellent tutorials on it. But anyway, the DOS Header is composed of a IMAGE_DOS_HEADER structure, so you want to use an IMAGE_DOS_HEADER structure when accessing the DOS Header.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 10:18 am    Post subject: Reply with quote

to access it, you would typecast your pointer as a pointer to the above structure then yes, you would be able to access it normally
Back to top
View user's profile Send private message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Sun Aug 23, 2009 2:17 pm    Post subject: Reply with quote

smartz993 wrote:
Yes.

but there's no reason to set the "magic" property of the struct again, after you already copied the memory lol


I've copied it into memory and the point of this post isn't directly to modify the "magic" property, but rather if I can copy that into a structure by what I said beforehand.

oib111 wrote:
I think you should read up on the PE File Format a bit more. Iczelion has some excellent tutorials on it. But anyway, the DOS Header is composed of a IMAGE_DOS_HEADER structure, so you want to use an IMAGE_DOS_HEADER structure when accessing the DOS Header.


Um, I know what wht dos header is composed of, the point isn't to read or access the dos header, its just what I used cause its convieniently the first section...

I just mapped the file into memory with ReadFile, to my knowledge.. i can't access it normally just be using a header, its just a chunk of information to the computer...

Slugsnack wrote:
to access it, you would typecast your pointer as a pointer to the above structure then yes, you would be able to access it normally


you mean typecast dos_header as a pointer or initialize another as a pointer to dos_header and then i can use it normally?
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 2:33 pm    Post subject: Reply with quote

okay what you need to do is get a pointer to PE header. then typecast that to IMAGE_NT_HEADERS*

access the .OptionalHeader of that which is an IMAGE_OPTIONAL_HEADER structure and your magic number, etc. etc. is right there

would you like an example in C ?
Back to top
View user's profile Send private message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Sun Aug 23, 2009 5:21 pm    Post subject: Reply with quote

Yeah, a code example would be nice,

EDIT: umm woops. I just remembered that the pe header starts 64 bytes after.

Thought i just realized that I shouldn't even allocate with GlobalAlloc, I just used malloc, I think I'm going to see if I can define the structures and just copy them using memmcpy, though I would appreciate the code.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Aug 24, 2009 4:44 am    Post subject: Reply with quote

Code:
#include <windows.h>
#include <stdio.h>
#include <conio.h>

int main()
{
  IMAGE_DOS_HEADER* pIDH = (IMAGE_DOS_HEADER*)GetModuleHandle( NULL );
  IMAGE_NT_HEADERS* pINH = (IMAGE_NT_HEADERS*)((BYTE*)pIDH + (pIDH -> e_lfanew));
  IMAGE_OPTIONAL_HEADER IOH = pINH -> OptionalHeader;

  printf( "Magic number is : %u\n", IOH.Magic );
  printf( "Address of entry point is : %#x", IOH.AddressOfEntryPoint );

  while( !_kbhit() )
    Sleep(100);

  return 0;
}


or did you want it to map a disk file into memory and access it that way ? if that is the case, there are plenty of examples out there for doing just that. eg :

http://msdn.microsoft.com/en-us/magazine/ms809762.aspx

this will also be useful to you :
895.zwit.org
Back to top
View user's profile Send private message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Mon Aug 24, 2009 8:40 am    Post subject: Reply with quote

tyvm slugsnack, eventually I did this last night:

Code:

memcpy(&HEADER_DOS,(PIMAGE_DOS_HEADER)file,sizeof(IMAGE_DOS_HEADER));
   memcpy(HEADER_NT,(PIMAGE_NT_HEADERS)((LPSTR)file + HEADER_DOS.e_lfanew),sizeof(PIMAGE_NT_HEADERS));


I'm sure to have other issues, and I'll read over the 2 articles, but I already know what a pe file looks like O.O
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Aug 24, 2009 9:27 am    Post subject: Reply with quote

actually i'm a bit confused as to what you're actually doing. since you are using memcpy i assume the PE header is already mapped into memory in which case you can just do some typecasting and access the fields straight off
Back to top
View user's profile Send private message
azfk
Cheater
Reputation: 0

Joined: 26 May 2009
Posts: 37

PostPosted: Wed Aug 26, 2009 2:11 pm    Post subject: Reply with quote

Yes, it was mapped into memory, I didn't realize I could do that however until later
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites