title: Loading PE Headers
body: <p>Basically, what I am trying to do is to find last section of PE file. I have read PE specification very attentively, yet I can't discover where my code fails.</p>
  
  <pre><code>PIMAGE_DOS_HEADER pidh = (PIMAGE_DOS_HEADER)buffer;
  PIMAGE_NT_HEADERS pinh = (PIMAGE_NT_HEADERS)(pidh + pidh-&gt;e_lfanew);
  PIMAGE_FILE_HEADER pifh = (PIMAGE_FILE_HEADER)&amp;pinh-&gt;FileHeader;
  PIMAGE_OPTIONAL_HEADER pioh = (PIMAGE_OPTIONAL_HEADER)&amp;pinh-&gt;OptionalHeader;
  PIMAGE_SECTION_HEADER pish = (PIMAGE_SECTION_HEADER)(pinh + sizeof(IMAGE_NT_HEADERS) + (pifh-&gt;NumberOfSections - 1) * sizeof(IMAGE_SECTION_HEADER));
  </code></pre>
  
  <p><code>buffer</code> is a byte array containing loaded executable, and <code>pish</code> is a pointer to the last section. For some reason, it appears that number of sections is over 20 000.</p>
  
  <p>Any ideas ?
  Thanks in advance</p>