Re: Reading PE-structure.
e_lfanew is the byte offset from the beginning of the data. You're trying to add it to the pointer type IMAGE_DOS_HEADER which will treat it as array of IMAGE_DOS_HEADER. It should be
Code:
IMAGE_NT_HEADERS *inh = (IMAGE_NT_HEADERS*)buffer + idh->e_lfanew;
Re: Reading PE-structure.
Just a guess, but this:
Code:
IMAGE_NT_HEADERS *inh = (IMAGE_NT_HEADERS*)idh+idh->e_lfanew;
could be your problem. If an IMAGE_NT_HEADERS isn't the same size as an IMAGE_DOS_HEADER, then the pointer arithmetic you're attempting might not be doing what you think. You'd better use parens to make it clearer whether you want to increment idh by idh->e_lfanew*sizeof(IMAGE_DOS_HEADER), idh->e_lfanew*sizeof(IMAGE_NT_HEADERS), or idh->e_lfanew*sizeof(char).
Re: Reading PE-structure.
Thanks for the suggestion but nothing seem to work.
I always end up with the same wrong signature.
The annoying thing is that I have done this before, or rather I have done but without the file reading. I got the address of a loaded module with GetModuleHandle and used the exact same logic, it worked like a charm.
Annoying.
Re: Reading PE-structure.
Okay I managed it, as you guys said it was my pointer arithmetic that was wrong. For future reference this is how it should have been done:
Code:
IMAGE_NT_HEADERS *inh = (IMAGE_NT_HEADERS*)((buffer)+(idh->e_lfanew));
Thanks again.
Re: Reading PE-structure.
Yes, my mistake :D It should not be typecasted to IMAGE_NT_HEADERS before adding the offset. It could also be this way
Code:
IMAGE_NT_HEADERS *inh = (IMAGE_NT_HEADERS*)&buffer[idh->e_lfanew];