How do i determine exact PE image file size using its header(s)?

OnTheFly picture OnTheFly · Nov 19, 2011 · Viewed 7.9k times · Source

I need byte size, IMAGE_OPTIONAL_HEADER.SizeOfImage appears to be rounded up to (unsure) boundary and is greater than real file size.

Answer

pezcode picture pezcode · Nov 19, 2011

IMAGE_OPTIONAL_HEADER.SizeOfImage is the size of the loaded executable/dll in virtual memory. It is not the same as the size on disk.

You can calculate it with VirtualAddress + VirtualSize of the last section.

IMAGE_OPTIONAL_HEADER.SizeOfImage is that value rounded up to the value of IMAGE_OPTIONAL_HEADER.SectionAlignment (usually the same as the page size).